package com.ztgf.shopping.service.shop;

//import com.codingapi.txlcn.tc.annotation.LcnTransaction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ztgf.commodity.models.dto.BrandInfoAuditDTO;
import com.ztgf.commodity.models.dto.BrandInfoDTO;
import com.ztgf.commodity.models.dto.CommodityCategoryDTO;
import com.ztgf.commodity.models.dto.CommodityInfoDTO;
import com.ztgf.commodity.models.service.*;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.common.utils.PinYinUtils;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.message.model.bo.MessageAnnouncementSaveBO;
import com.ztgf.message.model.constant.MessageTopicType;
import com.ztgf.message.model.dto.ResponseDTO;
import com.ztgf.message.model.enums.MessageEnum;
import com.ztgf.message.model.service.IKafkaProducerService;
import com.ztgf.order.models.dto.OrderCommodityRelationDTO;
import com.ztgf.order.models.dto.OrderStatusDTO;
import com.ztgf.order.models.service.IOrderCommodityRelationService;
import com.ztgf.order.models.service.IOrderInfoService;
import com.ztgf.order.models.service.IOrderStatusService;
import com.ztgf.pay.models.bo.PayUserSaveBO;
import com.ztgf.pay.models.dto.PayUserDTO;
import com.ztgf.pay.models.service.IPayUserService;
import com.ztgf.search.models.dto.EsShopDTO;
import com.ztgf.search.models.service.IShopSearchService;
import com.ztgf.search.models.service.SearchService;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.shop.ShopInfoAuditLogMapper;
import com.ztgf.shopping.models.bo.shop.*;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.dto.audit.AuditLogDTO;
import com.ztgf.shopping.models.dto.shop.*;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.audit.IAuditLogService;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.shopping.models.service.shop.IShopInfoAuditLogService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.template.ITempletInfoService;
import com.ztgf.shopping.models.vo.audit.RegisterShopVO;
import com.ztgf.shopping.models.vo.audit.RegisterVO;
import com.ztgf.shopping.models.vo.shop.ShopInfoAuditLogVO;
import com.ztgf.sms.models.bo.SmsInfoSaveBO;
import com.ztgf.sms.models.service.ISmsInfoService;
import com.ztgf.sys.models.dto.SysDictDTO;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.sys.models.service.ISysDictService;
import com.ztgf.sys.models.service.ISysUserService;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.dto.UserLoginDTO;
import com.ztgf.user.models.service.IUserInfoService;
import com.ztgf.user.models.service.IUserLoginService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 店铺审核日志控制器
 *
 * @author zhanghualang
 * @Date 2020-06-16 16:21:25
 */
@DubboService
@Slf4j
public class ShopInfoAuditLogServiceImpl extends ServiceImpl<ShopInfoAuditLogMapper, ShopInfoAuditLogDTO> implements IShopInfoAuditLogService {

    @Value(value = "${sms-url}")
    private String smsUrl;

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private ShopQualificationAuditServiceImpl iShopQualificationAuditService;
    @Autowired
    private ShopInfoAuditServiceImpl iShopInfoAuditService;
    @DubboReference
    private IUserInfoService iUserInfoService;
    @Autowired
    @Lazy
    private ShopInfoServiceImpl iShopInfoService;
    @Autowired
    private ShopBlacklistServiceImpl iShopBlacklistService;
    @Autowired
    private ShopUserRelationServiceImpl iShopUserRelationService;
    @Autowired
    private ShopQualificationServiceImpl iShopQualificationService;
    @DubboReference
    private IBrandInfoAuditService iBrandInfoAuditService;
    @Autowired
    private ShopBrandRelationAuditServiceImpl iShopBrandRelationAuditService;
    @Autowired
    private ShopCategoryRelationAuditServiceImpl iShopCategoryRelationAuditService;
    @DubboReference
    private IBrandInfoService iBrandInfoService;
    @Autowired
    private ShopBrandRelationServiceImpl iShopBrandRelationService;
    @Autowired
    private ShopCategoryRelationServiceImpl iShopCategoryRelationService;
    @DubboReference
    private IKafkaProducerService kafkaProducerService;
    @Autowired
    private ShopEarnestMoneyServiceImpl iShopEarnestMoneyService;
    @Autowired
    private ShopAnnualFeeServiceImpl iShopAnnualFeeService;
    @Autowired
    private ShopQiyuesuoInfoServiceImpl iShopQiyuesuoInfoService;
    @DubboReference
    private IUserLoginService iUserLoginService;
    @DubboReference
    private ISysUserService iSysUserService;
    @DubboReference
    private ISysDictService iSysDictService;
    @DubboReference
    private ISmsInfoService iSmsInfoService;
    @DubboReference
    private ITempletInfoService templetInfoService;
    @Autowired
    private ShopContractParamRelationServiceImpl shopContractParamRelationService;
    @DubboReference
    private IPayUserService iPayUserService;
    @Autowired
    private ShopContractSettleInServiceImpl shopContractSettleInService;
    @Autowired
    private ShopReceiveCardServiceImpl shopReceiveCardService;
    @Autowired
    private IAuditService auditService;
    @DubboReference
    private ICategoryBrandAttributesRelationService iCategoryBrandAttributesRelationService;
    @Autowired
    private ShopSalesAreaServiceImpl shopSalesAreaService;
    @DubboReference
    private IShopSearchService shopSearchService;
    @Autowired
    private IAuditLogService auditLogService;
    @DubboReference
    private ICommodityInfoService commodityInfoService;
    @DubboReference
    private SearchService searchService;
    @DubboReference
    private IOrderStatusService orderStatusService;
    @DubboReference
    private IOrderCommodityRelationService orderCommodityRelationService;
    @DubboReference
    private IOrderInfoService orderInfoService;
//    @DubboReference
//    private IShopInfoService shopInfoService;
    @DubboReference
    private ICommodityCategoryService commodityCategoryService;

    @Autowired
    private RedisUtil redisUtil;


    @Override
    //@Transactional
    //@LcnTransaction
    public boolean addLoginPass(ShopInfoAuditLoginSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_PASS.getCode();
        addLogin(saveBO, type, status);

        /*
         * 通过审核
         * 1.保存营业执照审核结果
         * 2.保存店铺审核接口
         * 3.保存用户审核结果
         * 4.增加店铺表及其资质表
         */

        /*
         * 123
         */
        //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
        updateAudit(saveBO, status);
        /*
         * 4增加店铺表，店员表及其资质表
         */
        updateShopInfo(saveBO);
        return true;
    }

    @Override
    //@Transactional
    //@LcnTransaction
    public boolean insertLoginReturn(ShopInfoAuditLoginSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_RETURM.getCode();
        addLogin(saveBO, type, status);

        /*
         * 打回审核
         * 1.保存营业执照审核结果
         * 2.保存买家审核结果
         * 3.保存用户审核结果
         */
        //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
        updateAudit(saveBO, status);
        return true;
    }

    @Override
    //@Transactional
    //@LcnTransaction
    public boolean insertLoginReject(ShopInfoAuditLoginSaveBO saveBO) {
        //事务预留
        //保存审核日志表
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_REJECT.getCode();
        addLogin(saveBO, type, status);

        /*
         * 拒绝审核，加入黑名单
         * 1.保存营业执照审核结果
         * 2.保存买家审核结果
         * 3.保存用户审核结果
         */
        //加入黑名单
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", saveBO.getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        ShopInfoAuditDTO dto = iShopInfoAuditService.selectById(saveBO.getShopAuditId());
        ShopBlacklistDTO shopBlacklistDTO = new ShopBlacklistDTO();
        shopBlacklistDTO.setCreateUserId(saveBO.getCreateUserId());
        shopBlacklistDTO.setShopInfoAuditId(saveBO.getShopAuditId());
        shopBlacklistDTO.setMobile(userLoginDTO.getMobile());
        shopBlacklistDTO.setCompanyName(dto.getName());
        shopBlacklistDTO.setType(ShoppingCodeEnum.BLACKLIST_LOGIN.getCode());
        shopBlacklistDTO.setId(ymlConfig.getPkId());
        iShopBlacklistService.insert(shopBlacklistDTO);

        //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
        updateAudit(saveBO, status);
        return true;
    }

    @Override
    //@Transactional
//    @LcnTransaction
    public boolean insertOpenPass(ShopInfoAuditOpenSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_PASS.getCode();
        addOpen(saveBO, type, status);

        /*
         * 通过审核
         * 1.保存资质审核结果
         * 2.保存品牌分类审核结果
         * 3.保存店铺审核
         * 4.保存品牌关联表审核
         * 5.更改店铺状态，增加资质表 若有新增品牌，增加品牌表
         */
        //保存开户银行
        addBrank(saveBO.getShopAuditId());
        //保存各资质审核结果
        updateQAuditList(saveBO.getShopQualificationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //保存品牌分类审核结果
        updateCRAuditList(saveBO.getShopCategoryRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //保存店铺审核
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        //保存关联表审核
        updateBRAudit(saveBO.getShopBrandRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //增加资质表
        addQualification(saveBO);
        //增加默认店铺模板
        templetInfoService.createDefaultTemplateByShopId(saveBO.getShopId());
        //增加默认的店铺合同模板
        shopContractParamRelationService.createDefaultShopContractInfo(saveBO.getShopId());
        //增加入住合同
        addContractSettleIn(saveBO.getShopId());

        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", iShopInfoService.selectById(saveBO.getShopId()).getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        addOpenSms(userLoginDTO.getMobile(), 0); //发送短信

        return urgeSignContact("您的开店申请已通过！", saveBO.getShopId(), ShoppingCodeEnum.MERCHANT_SELL.getCode());
    }

    @Override
    //@Transactional
//    @LcnTransaction
    public boolean insertOpenReturn(ShopInfoAuditOpenSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_RETURM.getCode();
        boolean flag = addOpen(saveBO, type, status);
        if (!flag) {
            return flag;
        }
        //保存各资质审核结果
        updateQAuditList(saveBO.getShopQualificationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //保存品牌分类审核结果
        updateCRAuditList(saveBO.getShopCategoryRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //保存店铺审核
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        //保存关联表审核
        updateBRAudit(saveBO.getShopBrandRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //更新店铺状态信息
        updateShopInfo(saveBO, status);

        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", iShopInfoService.selectById(saveBO.getShopId()).getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        addOpenSms(userLoginDTO.getMobile(), 1); //发送短信

        return urgeSignContact("您的开店申请已打回，审核建议为：" + saveBO.getContent(), saveBO.getShopId(), ShoppingCodeEnum.MERCHANT_BUYER.getCode());
    }

    @Override
    //@Transactional
//    @LcnTransaction
    public boolean insertOpenReject(ShopInfoAuditOpenSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_REJECT.getCode();
        boolean flag = addOpen(saveBO, type, status);
        if (!flag) {
            return flag;
        }
        //保存各资质审核结果
        updateQAuditList(saveBO.getShopQualificationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //保存品牌分类审核结果
        updateCRAuditList(saveBO.getShopCategoryRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //保存店铺审核
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        //保存关联表审核
        updateBRAudit(saveBO.getShopBrandRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //加入开店黑名单
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", iShopInfoService.selectById(saveBO.getShopId()).getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        ShopBlacklistDTO shopBlacklistDTO = new ShopBlacklistDTO();
        shopBlacklistDTO.setCreateUserId(saveBO.getCreateUserId());
        shopBlacklistDTO.setMobile(userLoginDTO.getMobile());
        shopBlacklistDTO.setShopId(saveBO.getShopId());
        shopBlacklistDTO.setCompanyName(iShopInfoService.selectById(saveBO.getShopId()).getName());
        shopBlacklistDTO.setType(ShoppingCodeEnum.BLACKLIST_OPEN.getCode());
        shopBlacklistDTO.setShopInfoAuditId(saveBO.getShopAuditId());
        shopBlacklistDTO.setId(ymlConfig.getPkId());
        iShopBlacklistService.insert(shopBlacklistDTO);
        //更新店铺状态信息
        updateShopInfo(saveBO, status);
        addOpenSms(userLoginDTO.getMobile(), 1); //发送短信

        return urgeSignContact("您的开店申请已拒绝", saveBO.getShopId(), ShoppingCodeEnum.MERCHANT_BUYER.getCode());
    }

    @Override
    public boolean insertChangePass(ShopInfoAuditChangeSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_CHANGE.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_PASS.getCode();
        boolean flag = addChange(saveBO, type, status);
        if (!flag) {
            return flag;
        }

        /*
         * 1修改店铺审核表信息
         * 2修改资质审核表信息
         * 3更新资质
         */
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        updateChangeAudit(saveBO, status);
        updateQualification(saveBO.getShopAuditId(), saveBO.getCreateUserId(), type);
        //解冻
        iShopInfoService.autoUnfreeze(saveBO.getShopId());
        Integer merchantType = iShopInfoService.selectById(saveBO.getShopId()).getMerchantType();
        boolean sendMessage = urgeSignContact("您的资质变更申请已通过！", saveBO.getShopId(), merchantType);
        return sendMessage;
    }


    @Override
    //@Transactional
    public boolean insertChangeReturn(ShopInfoAuditChangeSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_CHANGE.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_RETURM.getCode();
        boolean flag = addChange(saveBO, type, status);
        if (!flag) {
            return flag;
        }
        /*
         * 1修改店铺审核表信息
         * 2修改资质审核表信息
         */
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        updateChangeAudit(saveBO, status);
        Integer merchantType = iShopInfoService.selectById(saveBO.getShopId()).getMerchantType();
        return urgeSignContact("您的资质变更申请已打回，审核意见：" + saveBO.getContent(), saveBO.getShopId(), merchantType);
    }

    @Override
    //@Transactional
    public boolean insertBrandPass(ShopInfoAuditBrandSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_INSERTBRAND.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_PASS.getCode();
        boolean flag = addBrand(saveBO, type, status);
        if (!flag) {
            return flag;
        }
        /*
         * 1.修改店铺审核表信息
         * 2.修改关联审核表
         * 3.修改资质表审核
         * 4.更新关联表
         */
        //修改店铺审核表信息
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        //修改资质表审核
        updateQAuditList(saveBO.getShopQualificationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //修改关联审核表
        updateBRAuditSingle(saveBO.getShopAuditId(), saveBO.getBrandId(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //修改关联类目表
        updateBCAuditList(saveBO.getShopCategoryRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);

        //更新品牌相关
        addBrandQualification(saveBO);

        return urgeSignContact("您的新增品牌申请已通过！", saveBO.getShopId(), ShoppingCodeEnum.MERCHANT_SELL.getCode());
    }


    @Override
    //@Transactional
    public boolean insertBrandReturn(ShopInfoAuditBrandSaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_INSERTBRAND.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_RETURM.getCode();
        boolean flag = addBrand(saveBO, type, status);
        if (!flag) {
            return flag;
        }

        //修改店铺审核表信息
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        //修改资质表审核
        updateQAuditList(saveBO.getShopQualificationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //修改关联审核表
        updateBRAuditSingle(saveBO.getShopAuditId(), saveBO.getBrandId(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //修改关联类目表
        updateBCAuditList(saveBO.getShopCategoryRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);

        return urgeSignContact("您的新增品牌申请已打回，审核意见：" + saveBO.getContent(), saveBO.getShopId(), ShoppingCodeEnum.MERCHANT_SELL.getCode());
    }

    @Override
    //@Transactional
    public boolean insertCategoryPass(ShopInfoAuditCategorySaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_INSERTCATEGORY.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_PASS.getCode();
        boolean flag = addCategory(saveBO, type, status);
        if (!flag) {
            return flag;
        }
        //修改店铺审核表信息
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        //修改关联审核表
        updateBRAuditCategory(saveBO.getShopCategoryRelationAuditId(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //修改分类审核表
        updateCRAuditList(saveBO.getShopCategoryRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);

        //新增分类
        addShopCategory(saveBO);
        return urgeSignContact("您的品牌新增分类申请已通过", saveBO.getShopId(), ShoppingCodeEnum.MERCHANT_SELL.getCode());
    }

    @Override
    //@Transactional
    public boolean insertCategoryReturn(ShopInfoAuditCategorySaveBO saveBO) {
        //事务预留
        //保存审核日志
        //type 0注册，1开店，2资质变更，3新增品牌，4新增类目")
        //status 0审核中(提交审核)，1审核通过，2审核打回，3审核不通过")
        Integer type = ShoppingCodeEnum.AUDIT_TYPE_INSERTCATEGORY.getCode();
        Integer status = ShoppingCodeEnum.AUDIT_RETURM.getCode();
        boolean flag = addCategory(saveBO, type, status);
        if (!flag) {
            return flag;
        }

        //修改店铺审核表信息
        updateShopAudit(saveBO.getShopAuditId(), saveBO.getCreateUserId(), status, saveBO.getContent());
        //修改关联审核表
        updateBRAuditCategory(saveBO.getShopCategoryRelationAuditId(), saveBO.getCreateUserId(), saveBO.getContent(), status);
        //修改分类审核表
        updateCRAuditList(saveBO.getShopCategoryRelationAuditDTOList(), saveBO.getCreateUserId(), saveBO.getContent(), status);

        return urgeSignContact("您的品牌新增分类申请已打回，审核意见：" + saveBO.getContent(), saveBO.getShopId(), ShoppingCodeEnum.MERCHANT_SELL.getCode());
    }

    @Override
    public Page<ShopInfoAuditLogVO> queryPageList(ShopInfoAuditLogBO bo) {
        Page<ShopInfoAuditLogVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        ArrayList<ShopInfoAuditLogVO> shopInfoAuditLogVOArraysList = new ArrayList<ShopInfoAuditLogVO>();
        List<ShopInfoAuditLogVO> vos = baseMapper.queryPageList(page, bo);
        for (ShopInfoAuditLogVO vo : vos) {
            ShopInfoAuditLogVO shopInfoAuditLogVO = new ShopInfoAuditLogVO();
            BeanUtils.copyProperties(vo, shopInfoAuditLogVO);
            UserInfoDTO dto = iUserInfoService.selectById(vo.getCreateUserId());
            if (dto != null) {
                shopInfoAuditLogVO.setUserName(dto.getContactName());
            } else {
                SysUserDTO sysUserDTO = iSysUserService.selectById(vo.getCreateUserId());
                if (sysUserDTO != null) {
                    shopInfoAuditLogVO.setUserName(sysUserDTO.getName());
                } else {
                    shopInfoAuditLogVO.setUserName("");
                }
            }

            shopInfoAuditLogVOArraysList.add(shopInfoAuditLogVO);
        }
        page.setRecords(shopInfoAuditLogVOArraysList);
        return page;
    }

    //保存注册审核日志
    private boolean addLogin(ShopInfoAuditLoginSaveBO loginSaveBO, Integer type, Integer status) {
        ShopInfoAuditLogDTO dto = new ShopInfoAuditLogDTO();
        BeanUtils.copyProperties(loginSaveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        dto.setType(type);
        dto.setStatus(status);
        return this.insert(dto);
    }

    //保存开店审核日志
    private boolean addOpen(ShopInfoAuditOpenSaveBO openSaveBO, Integer type, Integer status) {
        ShopInfoAuditLogDTO dto = new ShopInfoAuditLogDTO();
        BeanUtils.copyProperties(openSaveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        dto.setType(type);
        dto.setStatus(status);
        return this.insert(dto);
    }

    //保存资质变更审核日志
    private boolean addChange(ShopInfoAuditChangeSaveBO changeSaveBO, Integer type, Integer status) {
        ShopInfoAuditLogDTO dto = new ShopInfoAuditLogDTO();
        BeanUtils.copyProperties(changeSaveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        dto.setType(type);
        dto.setStatus(status);
        return this.insert(dto);
    }

    //保存品牌新增审核日志
    private boolean addBrand(ShopInfoAuditBrandSaveBO brandSaveBO, Integer type, Integer status) {
        ShopInfoAuditLogDTO dto = new ShopInfoAuditLogDTO();
        BeanUtils.copyProperties(brandSaveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        dto.setType(type);
        dto.setStatus(status);
        return this.insert(dto);
    }

    //保存品牌类目新增审核日志
    private boolean addCategory(ShopInfoAuditCategorySaveBO categorySaveBO, Integer type, Integer status) {
        ShopInfoAuditLogDTO dto = new ShopInfoAuditLogDTO();
        BeanUtils.copyProperties(categorySaveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        dto.setType(type);
        dto.setStatus(status);
        return this.insert(dto);
    }


    private void addBrank(Long shopAuditId) {
        ShopInfoAuditDTO shopInfoAuditDTO = iShopInfoAuditService.selectById(shopAuditId);
        ShopReceiveCardSaveBO shopReceiveCardSaveBO = new ShopReceiveCardSaveBO();
        shopReceiveCardSaveBO.setBankName(shopInfoAuditDTO.getBankName());
        shopReceiveCardSaveBO.setBankCardNo(shopInfoAuditDTO.getBankCardNo());
        shopReceiveCardSaveBO.setBankAddress(shopInfoAuditDTO.getBankAddress());
        shopReceiveCardSaveBO.setName(shopInfoAuditDTO.getName());
        shopReceiveCardSaveBO.setType(1);//账户类型 0其他，1开户账户 默认0
        shopReceiveCardService.add(shopReceiveCardSaveBO);
    }

    private void updateAudit(ShopInfoAuditLoginSaveBO saveBO, Integer status) {
        //更新资质审核表
        String[] ids = saveBO.getShopQualificationAuditIds().split(",");
        for (String id : ids) {
            ShopQualificationAuditDTO dto = new ShopQualificationAuditDTO();
            dto.setUpdateUserId(saveBO.getCreateUserId());
            dto.setId(Long.valueOf(id));
            dto.setStatus(status);
            if (saveBO.getContent() != null && !"".equals(saveBO.getContent())) {
                dto.setContent(saveBO.getContent());
            }
            iShopQualificationAuditService.updateById(dto);
        }

        /*
         *更新店铺审核表
         */
        ShopInfoAuditDTO infoAuditDTO = new ShopInfoAuditDTO();
        infoAuditDTO.setId(saveBO.getShopAuditId());
        infoAuditDTO.setStatus(status);
        if (saveBO.getContent() != null && !"".equals(saveBO.getContent())) {
            infoAuditDTO.setContent(saveBO.getContent());
        }
        infoAuditDTO.setUpdateUserId(saveBO.getCreateUserId());
        iShopInfoAuditService.updateById(infoAuditDTO);

        /*
         * 保存用户审核结果
         */
        UserInfoDTO userInfoDTO = iUserInfoService.selectById(saveBO.getUserId());

//        if (userInfoDTO == null){
//            return false;
//        }

        //发送短信
        Integer type = 1;
        if (status.equals(ShoppingCodeEnum.AUDIT_PASS.getCode())) {
            type = 0;
        }
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userInfoDTO.getId());
        addLoginSms(iUserLoginService.selectOne(wrapper).getMobile(), type);

        UserInfoDTO userDTO = new UserInfoDTO();
        userDTO.setId(userInfoDTO.getId());
        userDTO.setStatus(status);//状态 0创建，1审核通过，2审核打回，3审核不通过,4冻结、5删除
        userDTO.setUpdateUserId(saveBO.getCreateUserId());
        iUserInfoService.updateById(userDTO);
    }

    //保存资质审核列表
    private void updateQAuditList(String QAList, Long userId, String content, Integer status) {
        List<ShopQualificationAuditDTO> newQualificationSaveBOList = JSON.parseArray(QAList, ShopQualificationAuditDTO.class);
        for (ShopQualificationAuditDTO dto : newQualificationSaveBOList) {
            ShopQualificationAuditDTO newDTO = new ShopQualificationAuditDTO();
            newDTO.setId(dto.getId());
            newDTO.setUpdateUserId(userId);//系统人员
            if (content != null && !"".equals(content)) {
                newDTO.setContent(content);
            }

            //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
            //若状态为审核中，使用整体审核状态。
            if (dto.getStatus() == ShoppingCodeEnum.AUDIT_RETURM.getCode()) {
                newDTO.setStatus(dto.getStatus());
            } else {
                if (status == ShoppingCodeEnum.AUDIT_RETURM.getCode()) {
                    newDTO.setStatus(ShoppingCodeEnum.AUDIT_PASS.getCode());
                } else {
                    newDTO.setStatus(status);
                }
            }

            iShopQualificationAuditService.updateById(newDTO);
        }
    }

    //保存品牌分类审核结果
    private void updateCRAuditList(String SCRAList, Long userId, String content, Integer status) {
        List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = JSON.parseArray(SCRAList, ShopCategoryRelationAuditDTO.class);
        for (ShopCategoryRelationAuditDTO dto : shopCategoryRelationAuditDTOList) {
            ShopCategoryRelationAuditDTO newDTO = new ShopCategoryRelationAuditDTO();
            newDTO.setId(dto.getId());
            newDTO.setUpdateUserId(userId);//系统人员
            if (content != null && !"".equals(content)) {
                newDTO.setContent(content);
            }

            //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
            //若状态为审核中，使用整体审核状态。
            if (dto.getStatus() == ShoppingCodeEnum.AUDIT_RETURM.getCode()) {
                newDTO.setStatus(dto.getStatus());
            } else {
                if (status == ShoppingCodeEnum.AUDIT_RETURM.getCode()) {
                    newDTO.setStatus(ShoppingCodeEnum.AUDIT_PASS.getCode());
                } else {
                    newDTO.setStatus(status);
                }
            }

            iShopCategoryRelationAuditService.updateById(newDTO);
        }
    }

    //更新店铺状态
    private boolean updateShopInfo(ShopInfoAuditOpenSaveBO bo, Integer status) {
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        shopInfoDTO.setId(bo.getShopId());
        shopInfoDTO.setSellerStatus(status);
        return iShopInfoService.updateById(shopInfoDTO);
    }

    //保存品牌关联表的审核表
    private void updateBRAudit(String SBRAList, Long userId, String content, Integer status) {
        List<ShopBrandRelationAuditDTO> dtoList = JSON.parseArray(SBRAList, ShopBrandRelationAuditDTO.class);
        for (ShopBrandRelationAuditDTO dto : dtoList) {
            ShopBrandRelationAuditDTO newDTO = new ShopBrandRelationAuditDTO();
            newDTO.setId(dto.getId());
            newDTO.setUpdateUserId(userId);
            if (content != null && !"".equals(content)) {
                newDTO.setContent(content);
            }
            //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
            //若状态为审核中，使用整体审核状态。
            if (dto.getStatus() == ShoppingCodeEnum.AUDIT_RETURM.getCode()) {
                newDTO.setStatus(dto.getStatus());
            } else {
                if (status == ShoppingCodeEnum.AUDIT_RETURM.getCode()) {
                    newDTO.setStatus(ShoppingCodeEnum.AUDIT_PASS.getCode());
                } else {
                    newDTO.setStatus(status);
                }
            }

            iShopBrandRelationAuditService.updateById(newDTO);

            //若类型为新增，更新品牌审核表 品牌是否存在 0存在。1不存在 默认0
            if (dto.getBrandType() == ShoppingCodeEnum.AUDIT_BRAND_NULL.getCode()) {
                BrandInfoAuditDTO brandInfoAuditDTO = new BrandInfoAuditDTO();
                brandInfoAuditDTO.setId(dto.getBrandId());
                brandInfoAuditDTO.setUpdateUserId(userId);
                if (content != null && !"".equals(content)) {
                    brandInfoAuditDTO.setContent(content);
                }

                if (dto.getStatus() == ShoppingCodeEnum.AUDIT_RETURM.getCode()) {
                    brandInfoAuditDTO.setStatus(dto.getStatus());
                } else {
                    if (status == ShoppingCodeEnum.AUDIT_RETURM.getCode()) {
                        brandInfoAuditDTO.setStatus(ShoppingCodeEnum.AUDIT_PASS.getCode());
                    } else {
                        brandInfoAuditDTO.setStatus(status);
                    }
                }

                iBrandInfoAuditService.updateById(brandInfoAuditDTO);
            }
        }
    }

    //保存品牌关联表的审核表(单个)
    private boolean updateBRAuditSingle(Long shopAudit, Long brandId, Long userId, String content, Integer status) {
        Wrapper<ShopBrandRelationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("brand_id", brandId).eq("shop_audit_id", shopAudit).eq("type", ShoppingCodeEnum.AUDIT_TYPE_INSERTBRAND.getCode());
        ShopBrandRelationAuditDTO shopBrandRelationAuditDTO = iShopBrandRelationAuditService.selectOne(wrapper);

        ShopBrandRelationAuditDTO newDTO = new ShopBrandRelationAuditDTO();
        newDTO.setId(shopBrandRelationAuditDTO.getId());
        newDTO.setUpdateUserId(userId);
        if (content != null && !"".equals(content)) {
            newDTO.setContent(content);
        }
        //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
        newDTO.setStatus(status);
        iShopBrandRelationAuditService.updateById(newDTO);

        //若类型为新增，更新品牌审核表 品牌是否存在 0存在。1不存在 默认0
        if (shopBrandRelationAuditDTO.getBrandType() == ShoppingCodeEnum.AUDIT_BRAND_NULL.getCode()) {
            BrandInfoAuditDTO brandInfoAuditDTO = new BrandInfoAuditDTO();
            brandInfoAuditDTO.setId(shopBrandRelationAuditDTO.getBrandId());
            brandInfoAuditDTO.setUpdateUserId(userId);
            if (content != null && !"".equals(content)) {
                brandInfoAuditDTO.setContent(content);
            }
            brandInfoAuditDTO.setStatus(status);
            iBrandInfoAuditService.updateById(brandInfoAuditDTO);
        }
        return true;
    }

    //保存店铺审核表
    private boolean updateShopAudit(Long auditId, Long userId, Integer status, String content) {
        ShopInfoAuditDTO dto = new ShopInfoAuditDTO();
        dto.setId(auditId);
        dto.setStatus(status);
        if (content != null && !"".equals(content)) {
            dto.setContent(content);
        }
        dto.setUpdateUserId(userId);
        return iShopInfoAuditService.updateById(dto);
    }

    private void updateChangeAudit(ShopInfoAuditChangeSaveBO saveBO, Integer status) {
        String[] ids = saveBO.getShopQualificationAuditIds().split(",");
        List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = iShopQualificationAuditService.selectBatchIds(Arrays.asList(ids));
        for (ShopQualificationAuditDTO dto : shopQualificationAuditDTOList) {
            ShopQualificationAuditDTO newDTO = new ShopQualificationAuditDTO();
            newDTO.setId(dto.getId());
            newDTO.setUpdateUserId(saveBO.getCreateUserId());//系统人员
            if (saveBO.getContent() != null && !"".equals(saveBO.getContent())) {
                newDTO.setContent(saveBO.getContent());
            }
            newDTO.setStatus(status);
            iShopQualificationAuditService.updateById(newDTO);
        }

        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", iShopInfoService.selectById(saveBO.getShopId()).getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        addChangeSms(userLoginDTO.getMobile());
    }

    private boolean updateShopInfo(ShopInfoAuditLoginSaveBO saveBO) {
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", saveBO.getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);

        /*
         * 保存店铺信息表
         */
        ShopInfoAuditDTO shopInfoAuditDTO = iShopInfoAuditService.selectById(saveBO.getShopAuditId());
        String legalPerson = shopInfoAuditDTO.getLegalPerson();
        String creditCode = shopInfoAuditDTO.getCreditCode();

        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        Long shopInfoId = ymlConfig.getPkId();
        shopInfoDTO.setId(shopInfoId);
        shopInfoDTO.setUserId(saveBO.getUserId());//保存用户ID
        shopInfoDTO.setName(shopInfoAuditDTO.getName());//公司名称
        shopInfoDTO.setAddress(shopInfoAuditDTO.getAddress());//保存公司地址
        shopInfoDTO.setInvitationCode(shopInfoAuditDTO.getInvitationCode());//邀请码
        shopInfoDTO.setLegalPerson(legalPerson);//法人
        shopInfoDTO.setIdCard(shopInfoAuditDTO.getIdCard());//法人身份证号
        shopInfoDTO.setIdCardType(shopInfoAuditDTO.getIdCardType());//法人证件类型
        shopInfoDTO.setCreditCode(creditCode);//统一信用代码
        shopInfoDTO.setPayMobile(shopInfoAuditDTO.getPayMobile());
        iShopInfoService.insert(shopInfoDTO);//店铺类型默认为买家

        /*
         * 保存店员表
         */
        ShopUserRelationDTO shopUserRelationDTO = new ShopUserRelationDTO();
        Long shopUserRelationId = ymlConfig.getPkId();
        shopUserRelationDTO.setId(shopUserRelationId);
        shopUserRelationDTO.setUserId(saveBO.getUserId());
        shopUserRelationDTO.setShopId(shopInfoId);
        iShopUserRelationService.insert(shopUserRelationDTO);

        /*
         * 保存店铺资质表
         */
        String[] ids = saveBO.getShopQualificationAuditIds().split(",");
        List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = iShopQualificationAuditService.selectBatchIds(Arrays.asList(ids));

        for (ShopQualificationAuditDTO shopQualificationAuditDTO : shopQualificationAuditDTOList) {
            ShopQualificationDTO shopQualificationDTO = new ShopQualificationDTO();
            BeanUtils.copyProperties(shopQualificationAuditDTO, shopQualificationDTO);
            shopQualificationDTO.setShopId(shopInfoId);
            shopQualificationDTO.setId(ymlConfig.getPkId());
            iShopQualificationService.insert(shopQualificationDTO);
        }


        /*
         * 保存电子签章 debug
         */
        addQiyuesuo(shopInfoAuditDTO.getName(), userLoginDTO.getMobile(), shopInfoId, legalPerson, creditCode);

        //中金开户
        ciccOpen(shopInfoDTO, userLoginDTO);
        return urgeSignContact("您的注册审核已通过！", shopInfoId, ShoppingCodeEnum.MERCHANT_BUYER.getCode());
    }

    //更新资质
    private boolean updateQualification(Long shopAudit, Long userId, Integer type) {
        List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = iShopQualificationAuditService.queryListByShopAudit(shopAudit, ShoppingCodeEnum.AUDIT_PASS.getCode(), type);
        for (ShopQualificationAuditDTO QAuditDTO : shopQualificationAuditDTOList) {
            Wrapper<ShopQualificationDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("shop_id", QAuditDTO.getShopId()).eq("type", QAuditDTO.getType());
            if (QAuditDTO.getBrandId() != null) {
                wrapper.eq("brand_id", QAuditDTO.getBrandId());
            }

            ShopQualificationDTO shopQualificationDTO = iShopQualificationService.selectOne(wrapper);
            if (shopQualificationDTO == null) {
                continue;
            }
            ShopQualificationDTO newDTO = new ShopQualificationDTO();
            newDTO.setId(shopQualificationDTO.getId());
            newDTO.setFileUrl(QAuditDTO.getFileUrl());//地址
            newDTO.setType(shopQualificationDTO.getType());
            newDTO.setShopId(shopQualificationDTO.getShopId());
            if (QAuditDTO.getBeginDate() != null) {
                newDTO.setBeginDate(QAuditDTO.getBeginDate());
            } else {
                newDTO.setBeginDate(null);
            }

            if (QAuditDTO.getEndDate() != null) {
                newDTO.setEndDate(QAuditDTO.getEndDate());
            } else {
                newDTO.setEndDate(null);
            }
            newDTO.setExpireStatus(ShoppingCodeEnum.EXPIRE_DEFAULT.getCode());
            newDTO.setUpdateUserId(userId);//更新人

            newDTO = updateSbQualificationDTO(newDTO);

            iShopQualificationService.updateById(newDTO);
        }
        return true;
    }

    private void addQualification(ShopInfoAuditOpenSaveBO bo) {
        /*
         * 1修改店铺状态
         * 2保存资质表
         * 3保存店铺品牌关系表
         * 4.保存品牌表
         */
        //修改店铺信息
        ShopInfoAuditDTO shopInfoAuditDTO = iShopInfoAuditService.selectById(bo.getShopAuditId());
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        shopInfoDTO.setId(bo.getShopId());
        shopInfoDTO.setSellerStatus(ShoppingCodeEnum.AUDIT_PASS.getCode());//开店审核状态 0创建、1审核通过、2审核打回、3，审核不通过。4重新提交。
        shopInfoDTO.setDomain(shopInfoAuditDTO.getDomain());//二级域名
        shopInfoDTO.setType(shopInfoAuditDTO.getShopType());//店铺类型 0专营店、1专卖店、2旗舰店、3自营店
        shopInfoDTO.setShopName(shopInfoAuditDTO.getShopName());//店铺名称
        shopInfoDTO.setShopAbbreviation(shopInfoAuditDTO.getShopAbbreviation());//店铺简称
        shopInfoDTO.setShopRealName(shopInfoAuditDTO.getShopRealName());//店铺本名
        shopInfoDTO.setInvitationCode(shopInfoAuditDTO.getInvitationCode());//邀请码
        shopInfoDTO.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(shopInfoAuditDTO.getShopName()));//店铺名首字母缩写
        shopInfoDTO.setMerchantType(ShoppingCodeEnum.MERCHANT_SELL.getCode());//店铺类别 0买家，1卖家

        /*
         * 保证金初始化
         */
        ShopEarnestMoneySaveBO shopEarnestMoneySaveBO = new ShopEarnestMoneySaveBO();
        if (bo.getEarnestMoney().compareTo(BigDecimal.ZERO) == 0) {
            shopEarnestMoneySaveBO.setStatus(ShoppingCodeEnum.MONEY_PAID.getCode()); //状态 0未缴纳，1已缴纳，2保证金不足
            shopInfoDTO.setEarnestMoney(BigDecimal.ZERO);
            shopInfoDTO.setMarginStatus(ShoppingCodeEnum.MONEY_PAID.getCode());//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
        } else {
            shopEarnestMoneySaveBO.setPayable(bo.getEarnestMoney());//应交
            shopEarnestMoneySaveBO.setVacancyAmount(bo.getEarnestMoney());//需补足
            shopInfoDTO.setEarnestMoney(BigDecimal.ZERO);
            shopEarnestMoneySaveBO.setStatus(ShoppingCodeEnum.MONEY_UNPAID.getCode()); //状态 0未缴纳，1已缴纳，2保证金不足
        }
        shopEarnestMoneySaveBO.setShopId(bo.getShopId());//店铺ID
        iShopEarnestMoneyService.add(shopEarnestMoneySaveBO);

        /*
         * 年费初始化
         */
        ShopAnnualFeeSaveBO shopAnnualFeeSaveBO = new ShopAnnualFeeSaveBO();
        if (bo.getAnnualFee().compareTo(BigDecimal.ZERO) == 0) {
            shopAnnualFeeSaveBO.setStatus(ShoppingCodeEnum.MONEY_PAID.getCode()); //状态 0未缴纳，1已缴纳，2保证金不足
            shopInfoDTO.setAnnualFeeStatus(ShoppingCodeEnum.MONEY_PAID.getCode());//年费 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
        } else {
            shopAnnualFeeSaveBO.setPayable(bo.getAnnualFee());//应交
            shopAnnualFeeSaveBO.setVacancyAmount(bo.getAnnualFee());//需补足
            shopAnnualFeeSaveBO.setStatus(ShoppingCodeEnum.MONEY_UNPAID.getCode()); //状态 0未缴纳，1已缴纳，2保证金不足
        }
        shopAnnualFeeSaveBO.setShopId(bo.getShopId());//店铺ID
        iShopAnnualFeeService.add(shopAnnualFeeSaveBO);

        iShopInfoService.updateById(shopInfoDTO);

        //保存基础资质表
        //审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        addBaseQualification(bo.getShopAuditId(), bo.getCreateUserId(), ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());

        //保存店铺关联表
        List<ShopBrandRelationAuditDTO> shopBrandRelationAuditDTOList = iShopBrandRelationAuditService.queryListByShopAudit(bo.getShopAuditId(), ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode(), ShoppingCodeEnum.AUDIT_PASS.getCode());
        for (ShopBrandRelationAuditDTO shopBrandRelationAuditDTO : shopBrandRelationAuditDTOList) {
            ShopBrandRelationDTO shopBrandRelationDTO = new ShopBrandRelationDTO();
            /*
             * 判断品牌是否存在，存在正常保存，不存在先保存品牌
             */
            Long brandId = shopBrandRelationAuditDTO.getBrandId();
            if (shopBrandRelationAuditDTO.getBrandType() == ShoppingCodeEnum.AUDIT_BRAND_NULL.getCode()) {
                //品牌是否存在 0存在。1不存在 默认0
                BrandInfoAuditDTO brandInfoAuditDTO = iBrandInfoAuditService.selectById(shopBrandRelationAuditDTO.getBrandId());
                //检测是否有同名的 没有就新增，有就用同名的id
                BrandInfoDTO oldBrandInfoDTO = iBrandInfoService.getByName(brandInfoAuditDTO.getName());
                if (oldBrandInfoDTO == null) {
                    BrandInfoDTO brandInfoDTO = new BrandInfoDTO();
                    brandId = ymlConfig.getPkId();
                    brandInfoDTO.setId(brandId);
                    brandInfoDTO.setName(brandInfoAuditDTO.getName());
                    brandInfoDTO.setLogoUrl(brandInfoAuditDTO.getLogoUrl());
                    brandInfoDTO.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(brandInfoAuditDTO.getName()));
                    brandInfoDTO.setIsPublish(1);//是否发布;0未发布，1发布
                    iBrandInfoService.insert(brandInfoDTO);
                } else {
                    brandId = oldBrandInfoDTO.getId();
                }
            }

            //保存关联表
            shopBrandRelationDTO.setId(ymlConfig.getPkId());
            shopBrandRelationDTO.setShopId(bo.getShopId());
            shopBrandRelationDTO.setBrandId(brandId);
            iShopBrandRelationService.insert(shopBrandRelationDTO);

            //保存品牌资质
            addBrandQualification(bo.getShopAuditId(), bo.getCreateUserId(), ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode(), brandId, shopBrandRelationAuditDTO.getBrandId());

            //保存品牌分类表
            List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = iShopCategoryRelationAuditService.queryListByShopAudit(bo.getShopAuditId(), shopBrandRelationAuditDTO.getBrandId(), ShoppingCodeEnum.AUDIT_PASS.getCode(), ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());
            for (ShopCategoryRelationAuditDTO shopCategoryRelationAuditDTO : shopCategoryRelationAuditDTOList) {
                ShopCategoryRelationDTO shopCategoryRelationDTO = new ShopCategoryRelationDTO();
                shopCategoryRelationDTO.setId(ymlConfig.getPkId());
                shopCategoryRelationDTO.setBrandId(brandId);//已经存在的id
                shopCategoryRelationDTO.setCategoryId(shopCategoryRelationAuditDTO.getCategoryId());
                shopCategoryRelationDTO.setShopId(bo.getShopId());
                shopCategoryRelationDTO.setCreateUserId(bo.getCreateUserId());
                shopCategoryRelationDTO.setUpdateUserId(bo.getCreateUserId());
                iShopCategoryRelationService.insert(shopCategoryRelationDTO);

                //保存关系单位表
                iCategoryBrandAttributesRelationService.addUnitidsByBrandAndCategory(brandId, shopCategoryRelationAuditDTO.getCategoryId());
            }
        }
    }

    //增加基础资质
    private void addBaseQualification(Long shopAuditId, Long userId, Integer type) {
        Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_audit_id", shopAuditId).eq("audit_type", type);
        wrapper.andNew().eq("type", ShoppingCodeEnum.QUALIFICATION_ACCOUNT.getCode());

        List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = iShopQualificationAuditService.selectList(wrapper);

        for (ShopQualificationAuditDTO shopQualificationAuditDTO : shopQualificationAuditDTOList) {
            ShopQualificationDTO shopQualificationDTO = new ShopQualificationDTO();
            BeanUtils.copyProperties(shopQualificationAuditDTO, shopQualificationDTO);
            shopQualificationDTO.setId(ymlConfig.getPkId());
            shopQualificationDTO.setCreateUserId(userId);//创建人
            shopQualificationDTO.setUpdateUserId(userId);//更新人
            shopQualificationDTO.setShopId(shopQualificationAuditDTO.getShopId());

            shopQualificationDTO = updateSbQualificationDTO(shopQualificationDTO);

            iShopQualificationService.insert(shopQualificationDTO);
        }
    }

    private ShopQualificationDTO updateSbQualificationDTO(ShopQualificationDTO shopQualificationDTO) {
        /**
         *     QUALIFICATION_TRADEMARK(3, "商标注册证书"),
         *     QUALIFICATION_RENEWAL(4, "续展证明文件"),
         */
        if (shopQualificationDTO.getType().equals(ShoppingCodeEnum.QUALIFICATION_TRADEMARK.getCode())) {
            //若是商标注册证书。结束时间改为续展证明的时间。
            Wrapper<ShopQualificationDTO> xzwrapper = new EntityWrapper<>();
            xzwrapper.eq("shop_id", shopQualificationDTO.getShopId()).eq("type", ShoppingCodeEnum.QUALIFICATION_RENEWAL.getCode());
            ShopQualificationDTO xzDto = iShopQualificationService.selectOne(xzwrapper);
            if (xzDto != null) {
                shopQualificationDTO.setEndDate(xzDto.getEndDate());
            }
        }

        if (shopQualificationDTO.getType().equals(ShoppingCodeEnum.QUALIFICATION_RENEWAL.getCode())) {
            //若是续展证明。将商标注册结束时间改为续展证明的时间。
            Wrapper<ShopQualificationDTO> sbwrapper = new EntityWrapper<>();
            sbwrapper.eq("shop_id", shopQualificationDTO.getShopId()).eq("type", ShoppingCodeEnum.QUALIFICATION_TRADEMARK.getCode());
            ShopQualificationDTO sbDto = iShopQualificationService.selectOne(sbwrapper);
            if (sbDto != null) {
                ShopQualificationDTO newsbDto = new ShopQualificationDTO();
                newsbDto.setId(sbDto.getId());
                newsbDto.setEndDate(shopQualificationDTO.getEndDate());
                iShopQualificationService.updateById(newsbDto);
            }
        }

        return shopQualificationDTO;
    }

    //保存资质表
    private boolean addBrandQualification(Long shopAuditId, Long userId, Integer type, Long brandId, Long brandAuditId) {
        Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_audit_id", shopAuditId).eq("audit_type", type);
        wrapper.eq("brand_id", brandAuditId);
        List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = iShopQualificationAuditService.selectList(wrapper);
        for (ShopQualificationAuditDTO shopQualificationAuditDTO : shopQualificationAuditDTOList) {
            ShopQualificationDTO shopQualificationDTO = new ShopQualificationDTO();
            BeanUtils.copyProperties(shopQualificationAuditDTO, shopQualificationDTO);
            shopQualificationDTO.setId(ymlConfig.getPkId());
            shopQualificationDTO.setBrandId(brandId);
            shopQualificationDTO.setCreateUserId(userId);//创建人
            shopQualificationDTO.setUpdateUserId(userId);//更新人
            iShopQualificationService.insert(shopQualificationDTO);
        }

        return true;
    }

    private boolean addBrandQualification(ShopInfoAuditBrandSaveBO saveBO) {
        /*
         * 1保存资质表
         * 2保存店铺品牌关系表
         * 3.保存品牌表
         */
        Wrapper<ShopBrandRelationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("brand_id", saveBO.getBrandId()).eq("shop_audit_id", saveBO.getShopAuditId()).eq("type", ShoppingCodeEnum.AUDIT_TYPE_INSERTBRAND.getCode()).eq("status", ShoppingCodeEnum.AUDIT_PASS.getCode());
        ShopBrandRelationAuditDTO shopBrandRelationAuditDTO = iShopBrandRelationAuditService.selectOne(wrapper);
        /*
         * 判断品牌是否存在，存在正常保存，不存在先保存品牌
         */
        Long brandId = shopBrandRelationAuditDTO.getBrandId();
        if (shopBrandRelationAuditDTO.getBrandType() == ShoppingCodeEnum.AUDIT_BRAND_NULL.getCode()) {
            //品牌是否存在 0存在。1不存在 默认0
            BrandInfoAuditDTO brandInfoAuditDTO = iBrandInfoAuditService.selectById(shopBrandRelationAuditDTO.getBrandId());
            //检测是否有同名的
            BrandInfoDTO oldBrandInfoDTO = iBrandInfoService.getByName(brandInfoAuditDTO.getName());
            if (oldBrandInfoDTO == null) {
                BrandInfoDTO brandInfoDTO = new BrandInfoDTO();
                brandId = ymlConfig.getPkId();
                brandInfoDTO.setId(brandId);
                brandInfoDTO.setName(brandInfoAuditDTO.getName());
                brandInfoDTO.setLogoUrl(brandInfoAuditDTO.getLogoUrl());
                brandInfoDTO.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(brandInfoAuditDTO.getName()));
                brandInfoDTO.setIsPublish(1);//是否发布;0未发布，1发布
                iBrandInfoService.insert(brandInfoDTO);
            } else {
                brandId = oldBrandInfoDTO.getId();
            }

        }

        //保存关联表
        //先验证是否已经新增过了
        Wrapper<ShopBrandRelationDTO> bwrapper = new EntityWrapper<>();
        bwrapper.eq("shop_id", saveBO.getShopId()).eq("brand_id", brandId);
        ShopBrandRelationDTO oldShopBrandRelationDTO = iShopBrandRelationService.selectOne(bwrapper);
        if (oldShopBrandRelationDTO != null && oldShopBrandRelationDTO.getId() != null) {
            return false;
        }
        ShopBrandRelationDTO shopBrandRelationDTO = new ShopBrandRelationDTO();
        shopBrandRelationDTO.setId(ymlConfig.getPkId());
        shopBrandRelationDTO.setShopId(saveBO.getShopId());
        shopBrandRelationDTO.setBrandId(brandId);
        iShopBrandRelationService.insert(shopBrandRelationDTO);

        //保存资质表
        //审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        addBrandQualification(saveBO.getShopAuditId(), saveBO.getCreateUserId(), ShoppingCodeEnum.AUDIT_TYPE_INSERTBRAND.getCode(), brandId, shopBrandRelationAuditDTO.getBrandId());

        //保存品牌分类表
        List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = iShopCategoryRelationAuditService.queryListByShopAudit(saveBO.getShopAuditId(), shopBrandRelationAuditDTO.getBrandId(), ShoppingCodeEnum.AUDIT_PASS.getCode(), ShoppingCodeEnum.AUDIT_TYPE_INSERTBRAND.getCode());
        for (ShopCategoryRelationAuditDTO shopCategoryRelationAuditDTO : shopCategoryRelationAuditDTOList) {
            ShopCategoryRelationDTO shopCategoryRelationDTO = new ShopCategoryRelationDTO();
            shopCategoryRelationDTO.setId(ymlConfig.getPkId());
            shopCategoryRelationDTO.setBrandId(brandId);//已经存在的id
            shopCategoryRelationDTO.setCategoryId(shopCategoryRelationAuditDTO.getCategoryId());
            shopCategoryRelationDTO.setShopId(saveBO.getShopId());
            shopCategoryRelationDTO.setCreateUserId(saveBO.getCreateUserId());
            shopCategoryRelationDTO.setUpdateUserId(saveBO.getCreateUserId());
            iShopCategoryRelationService.insert(shopCategoryRelationDTO);

            iCategoryBrandAttributesRelationService.addUnitidsByBrandAndCategory(brandId, shopCategoryRelationAuditDTO.getCategoryId());
        }
        return true;
    }

    private boolean updateBRAuditCategory(Long ShopCategoryRelationAuditId, Long userId, String content, Integer status) {
        ShopBrandRelationAuditDTO shopBrandRelationAuditDTO = iShopBrandRelationAuditService.selectById(ShopCategoryRelationAuditId);
        ShopBrandRelationAuditDTO newDTO = new ShopBrandRelationAuditDTO();
        newDTO.setId(shopBrandRelationAuditDTO.getId());
        newDTO.setUpdateUserId(userId);
        if (content != null && !"".equals(content)) {
            newDTO.setContent(content);
        }
        //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
        newDTO.setStatus(status);
        return iShopBrandRelationAuditService.updateById(newDTO);
    }

    private boolean addShopCategory(ShopInfoAuditCategorySaveBO saveBO) {
        //保存品牌分类表
        ShopBrandRelationAuditDTO shopBrandRelationAuditDTO = iShopBrandRelationAuditService.selectById(saveBO.getShopCategoryRelationAuditId());
        List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = iShopCategoryRelationAuditService.queryListByShopAudit(saveBO.getShopAuditId(), shopBrandRelationAuditDTO.getBrandId(), ShoppingCodeEnum.AUDIT_PASS.getCode(), ShoppingCodeEnum.AUDIT_TYPE_INSERTCATEGORY.getCode());
        for (ShopCategoryRelationAuditDTO shopCategoryRelationAuditDTO : shopCategoryRelationAuditDTOList) {
            ShopCategoryRelationDTO shopCategoryRelationDTO = new ShopCategoryRelationDTO();
            shopCategoryRelationDTO.setId(ymlConfig.getPkId());
            shopCategoryRelationDTO.setBrandId(shopCategoryRelationAuditDTO.getBrandId());//已经存在的id
            shopCategoryRelationDTO.setCategoryId(shopCategoryRelationAuditDTO.getCategoryId());
            shopCategoryRelationDTO.setShopId(saveBO.getShopId());
            shopCategoryRelationDTO.setCreateUserId(saveBO.getCreateUserId());
            shopCategoryRelationDTO.setUpdateUserId(saveBO.getCreateUserId());
            iShopCategoryRelationService.insert(shopCategoryRelationDTO);

            iCategoryBrandAttributesRelationService.addUnitidsByBrandAndCategory(shopCategoryRelationAuditDTO.getBrandId(), shopCategoryRelationAuditDTO.getCategoryId());
        }
        return true;
    }

    private void updateBCAuditList(String SCRAList, Long createUserId, String content, Integer status) {
        List<ShopCategoryRelationAuditDTO> shopCategoryRelationAuditDTOList = JSON.parseArray(SCRAList, ShopCategoryRelationAuditDTO.class);
        for (ShopCategoryRelationAuditDTO dto : shopCategoryRelationAuditDTOList) {
            ShopCategoryRelationAuditDTO shopCategoryRelationAuditDTO = new ShopCategoryRelationAuditDTO();
            shopCategoryRelationAuditDTO.setId(dto.getId());
            shopCategoryRelationAuditDTO.setUpdateUserId(createUserId);
            shopCategoryRelationAuditDTO.setStatus(status);
            shopCategoryRelationAuditDTO.setContent(content);
            iShopCategoryRelationAuditService.updateById(shopCategoryRelationAuditDTO);
        }
    }

    //发送消息 只有站内信
    private boolean urgeSignContact(String conunt, Long sellShopId, Integer shopType) {
        MessageAnnouncementSaveBO messageAnnouncementSaveBO = new MessageAnnouncementSaveBO();

        if (shopType.equals(ShoppingCodeEnum.MERCHANT_BUYER.getCode())) {
            //买家
            messageAnnouncementSaveBO.setShopType(MessageEnum.SHOP_TYPE_1.getCode());
        } else {
            messageAnnouncementSaveBO.setShopType(MessageEnum.SHOP_TYPE_2.getCode());
        }

        ResponseDTO responseDTO = new ResponseDTO();
        messageAnnouncementSaveBO.setContent(conunt);
        messageAnnouncementSaveBO.setShopId(sellShopId);
        messageAnnouncementSaveBO.setType(MessageEnum.SYS_MESSAGE.getCode());
        messageAnnouncementSaveBO.setTitle("审核结果通知");
        messageAnnouncementSaveBO.setJumpType(MessageEnum.MESSAGE_SHOW.getCode() + "");
        messageAnnouncementSaveBO.setTextType(1);
        messageAnnouncementSaveBO.setKey(sellShopId + "");
        responseDTO.setData(messageAnnouncementSaveBO);
        responseDTO.setMessageId(sellShopId + "");

        try {
            kafkaProducerService.senMessage(JSON.toJSONString(responseDTO), MessageTopicType.COMMENT_MESSAGE_TOPIC);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    //保存电子签章
    private void addQiyuesuo(String name, String mobile, Long shopId, String legalPerson, String creditCode) {
        ShopQiyuesuoInfoSaveBO shopQiyuesuoInfoSaveBO = new ShopQiyuesuoInfoSaveBO();
        shopQiyuesuoInfoSaveBO.setShopId(shopId);
        shopQiyuesuoInfoSaveBO.setCompanyName(name);
        shopQiyuesuoInfoSaveBO.setTenantName(name);
        shopQiyuesuoInfoSaveBO.setTenantType("COMPANY");
        shopQiyuesuoInfoSaveBO.setContact(mobile);
        shopQiyuesuoInfoSaveBO.setContactType("MOBILE");
        shopQiyuesuoInfoSaveBO.setTenantStamperType("COMPANY");
        shopQiyuesuoInfoSaveBO.setAuthenticationStatus("-1");
        shopQiyuesuoInfoSaveBO.setLegalPerson(legalPerson);
        shopQiyuesuoInfoSaveBO.setRegisterNo(creditCode);
        iShopQiyuesuoInfoService.add(shopQiyuesuoInfoSaveBO);
    }

    //发送短信
    private void addLoginSms(String mobile, Integer type) {
        //判断使用的是dubbo还是api
        SysDictDTO dto = iSysDictService.getByName(ShoppingConstant.DUBBO_OR_API);
        //0dubbo ,1api .默认0
        if (dto != null && dto.getValue().equals("1")) {
            String url = smsUrl + "/sms-client/api/sms/passAudit";
            if (type.equals(1)) {
                url = smsUrl + "/sms-client/api/sms/unPassAudit";
            }

            RestTemplate restTemplate = new RestTemplate();
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();//hashmap无法解析
            map.add("mobile", mobile);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);//fromdata 方式发送
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
            restTemplate.postForEntity(url, request, Result.class);
        } else {
            SmsInfoSaveBO bo = new SmsInfoSaveBO();
            bo.setMobile(mobile);
            try {
                if (type.equals(0)) {
                    iSmsInfoService.savePassAudit(bo);
                } else {
                    iSmsInfoService.saveUnPassAudit(bo);
                }
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
    }

    //发送短信
    private void addOpenSms(String mobile, Integer type) {
        //判断使用的是dubbo还是api
        SysDictDTO dto = iSysDictService.getByName(ShoppingConstant.DUBBO_OR_API);
        //0dubbo ,1api .默认0
        if (dto != null && dto.getValue().equals("1")) {
            String url = smsUrl + "/sms-client/api/sms/passOpenAudit";
            if (type.equals(1)) {
                url = smsUrl + "/sms-client/api/sms/unPassOpenAudit";
            }

            RestTemplate restTemplate = new RestTemplate();
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();//hashmap无法解析
            map.add("mobile", mobile);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);//fromdata 方式发送
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
            restTemplate.postForEntity(url, request, Result.class);
        } else {
            SmsInfoSaveBO bo = new SmsInfoSaveBO();
            bo.setMobile(mobile);
            try {
                if (type.equals(0)) {
                    iSmsInfoService.savePassOpenAudit(bo);
                } else {
                    iSmsInfoService.saveUnPassOpenAudit(bo);
                }
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
    }

    //发送短信
    private void addChangeSms(String mobile) {
        //判断使用的是dubbo还是api
        SysDictDTO dto = iSysDictService.getByName(ShoppingConstant.DUBBO_OR_API);
        //0dubbo ,1api .默认0
        if (dto != null && dto.getValue().equals("1")) {
            String url = smsUrl + "/sms-client/api/sms/qualificationChange";
            RestTemplate restTemplate = new RestTemplate();
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();//hashmap无法解析
            map.add("mobile", mobile);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);//fromdata 方式发送
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
            restTemplate.postForEntity(url, request, Result.class);
        } else {
            SmsInfoSaveBO bo = new SmsInfoSaveBO();
            bo.setMobile(mobile);
            try {
                iSmsInfoService.saveQualificationChange(bo);
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
    }

    private void addContractSettleIn(Long shopId) {
        ShopInfoDTO shopInfoDTO = iShopInfoService.selectById(shopId);

        String contractLockId = "";
        try {
            contractLockId = shopContractSettleInService.createContract(shopId);
        } catch (Exception e) {
            log.info("入驻协议，报错信息：" + e.getMessage());
            return;
        }
        ShopContractSettleInDTO dto = new ShopContractSettleInDTO();
        dto.setShopId(shopId);
        dto.setName(shopInfoDTO.getName());
        dto.setContractLockId(contractLockId);
        dto.setContractName(shopInfoDTO.getName() + "入驻协议");
        dto.setStatus(0);//0未签订，1已签订
        shopContractSettleInService.insert(dto);
    }

    private void ciccOpen(ShopInfoDTO shopInfoDTO, UserLoginDTO userLoginDTO) {
        PayUserSaveBO payUserSaveBO = new PayUserSaveBO();
        payUserSaveBO.setUserId(shopInfoDTO.getId());
        payUserSaveBO.setUserName(shopInfoDTO.getName());
        payUserSaveBO.setType(1);
        payUserSaveBO.setLegalName(shopInfoDTO.getLegalPerson());
        if (shopInfoDTO.getIdCardType().equals(0)) {
            payUserSaveBO.setLegalCredentialsType("A");
        } else {
            payUserSaveBO.setLegalCredentialsType("E");
        }
        payUserSaveBO.setLegalCredentialsNo(shopInfoDTO.getIdCard());
        payUserSaveBO.setBusinessLicenseNo(shopInfoDTO.getCreditCode());
        payUserSaveBO.setUserAddress(shopInfoDTO.getAddress());
        payUserSaveBO.setUserMobile(userLoginDTO.getMobile());
        payUserSaveBO.setUserEmail(userLoginDTO.getEmail());

        Result result = iPayUserService.add(payUserSaveBO);
        ShopInfoDTO newDTO = new ShopInfoDTO();
        newDTO.setId(shopInfoDTO.getId());
        if (result.getCode() == 0) {
            newDTO.setCiccStatus(1);//中金开户状态 0未开户，1已开户
            ObjectMapper objectMapper = new ObjectMapper();
            PayUserDTO dto = objectMapper.convertValue(result.getData(), PayUserDTO.class);
            newDTO.setZjMoneyNo(dto.getZjMoneyNo());
            newDTO.setZjBankName(dto.getZjBankName());
            iShopInfoService.updateById(newDTO);
        }
    }

    @Override
    public Result insertRegisterAudit(String loginVO) {
        RegisterVO registerVO = JSONObject.parseObject(loginVO, RegisterVO.class);
        RegisterVO.BaseInfoVO baseInfoVO = registerVO.getBase();
        RegisterVO.AuditInfoVO auditInfoVO = registerVO.getAudit();
        List<RegisterVO.QualificationInfoVO> qualificationInfoVOList = registerVO.getQualificationInfoVOList();
        //基本信息
        //资质信息
        //验证审核人ID
        String sysuId = auditInfoVO.getSysUserId().getValue();
        String uId = auditInfoVO.getUserId().getValue();
        String aId = auditInfoVO.getAuditId().getValue();
        String statusS = auditInfoVO.getStatus().getValue();
        String content = auditInfoVO.getContent().getValue();
        if (StringUtils.isEmpty(sysuId) || StringUtils.isEmpty(uId) || StringUtils.isEmpty(aId) || StringUtils.isEmpty(statusS)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Long sysUserId = Long.parseLong(sysuId);
        Long userId = Long.parseLong(uId);
        Long auditId = Long.parseLong(aId);
        Integer status = Integer.parseInt(statusS);

        AuditDTO dto = auditService.selectById(auditId);
        if (!dto.getStatus().equals(0) && !dto.getStatus().equals(4)) {
            return new Result<>(CodeEnum.FAIL_100046);
        }

        /*
         * 通过审核
         * 1.增加审核日志
         * 2.更新审核表
         * 3.更新用户
         * 4.新增店铺
         */
        //更新用户表
        updateUserInfo(sysUserId, userId, status);
        if (status.equals(1)) {
            //查询审核日志表
            AuditLogDTO auditLogDTO = auditLogService.getByAidAndStatus(auditId, status);
            if (auditLogDTO != null) {
                return new Result<>(CodeEnum.FAIL_100046);
            } else {
                auditLogService.add(auditId, status);//此时的操作状态是未通过
            }
            //新增店铺
            Long shopId = addShopInfo(baseInfoVO, userId);
            //新增店员表
            addShopUserRela(shopId, userId);
            //新增资质
            addRQualification(qualificationInfoVOList, shopId);
            //保存电子签章
            addQiyuesuo(baseInfoVO, shopId);
            //中金开户
            addCiccOpen(baseInfoVO, shopId);
            //发站内信
            sendurgeSignContact("您的注册审核已通过！", shopId, ShoppingCodeEnum.MERCHANT_BUYER.getCode());
            redisUtil.removeTokenId(userId, "0");
        }
        //更新审核表
        updateAudit(loginVO, sysUserId, auditId, status, content);
        //发短信
        sendLoginSms(baseInfoVO.getMobile().getValue(), status);
        if (status.equals(3)) {
            //加入黑名单
            addBlcakList(sysUserId, auditId, baseInfoVO.getMobile().getValue(), baseInfoVO.getName().getValue(), ShoppingCodeEnum.BLACKLIST_LOGIN.getCode(), null);
        }
        return new Result<>();
    }

    @Override
    public Result insertShopAudit(String shopVO) {
        RegisterShopVO registerShopVO = JSONObject.parseObject(shopVO, RegisterShopVO.class);
        RegisterShopVO.AuditInfoVO audit = registerShopVO.getAudit();//审核内容
        RegisterShopVO.BaseInfoVO base = registerShopVO.getBase();//基础信息
        List<RegisterShopVO.QualificationInfoVO> qualificationInfoVOList = registerShopVO.getQualificationInfoVOList();//基础资质
        List<RegisterShopVO.SalesAreaVO> salesAreaVOList = registerShopVO.getSalesAreaVOList();//可售区域
        List<RegisterShopVO.BrandAuditVO> brandAuditVOList = registerShopVO.getBrandAuditVOList();//品牌

        String suId = audit.getSysUserId().getValue();
        String aId = audit.getAuditId().getValue();
        String astatus = audit.getStatus().getValue();
        String sId = base.getShopId().getValue();
        String eMoney = audit.getEarnestMoney().getValue();
        String aFee = audit.getAnnualFee().getValue();
        String content = audit.getContent().getValue();

        if (StringUtil.isEmpty(suId) || StringUtil.isEmpty(astatus) || StringUtil.isEmpty(sId) || StringUtil.isEmpty(aId)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Long sysUserId = Long.parseLong(suId);//系统审核人员ID
        Integer status = Integer.parseInt(astatus);//审核状态
        Long shopId = Long.parseLong(sId);//店铺ID
        Long auditId = Long.parseLong(aId);//审核表ID

        AuditDTO dto = auditService.selectById(auditId);
        if (!dto.getStatus().equals(0) && !dto.getStatus().equals(4)) {
            return new Result<>(CodeEnum.FAIL_100046);
        }

        //查手机号
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", iShopInfoService.selectById(shopId).getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        String mobile = userLoginDTO.getMobile();

        String contact = "您的开店审核已通过！";
        int smsType = 0;
        int contactType = ShoppingCodeEnum.MERCHANT_SELL.getCode();
        if (status.equals(ShoppingCodeEnum.AUDIT_PASS.getCode())) {
            //判断是否包含审核不过的情况
            if (shopVO.contains("\"status\": \"0\"") || shopVO.contains("\"status\": \"2\"") || shopVO.contains("\"status\": \"3\"")) {
                return new Result<>(CodeEnum.FAIL_100052);
            }

            //查询审核日志表
            AuditLogDTO auditLogDTO = auditLogService.getByAidAndStatus(auditId, status);
            if (auditLogDTO != null) {
                return new Result<>(CodeEnum.FAIL_100046);
            } else {
                auditLogService.add(auditId, status);//此时的状态是未通过
            }

            //年费及保证金
            if (StringUtil.isEmpty(eMoney) || StringUtil.isEmpty(aFee)) {
                return new Result<>(CodeEnum.FAIL_100002);
            }
            BigDecimal earnestMoney = new BigDecimal(eMoney);
            BigDecimal annualFee = new BigDecimal(aFee);

            //保存开户银行
            addReceiveCard(base, shopId);
            //增加基本资质
            addShopQualification(qualificationInfoVOList, shopId, null);
            //保证金和年费初始化
            addAnnualAndEarnest(earnestMoney, annualFee, shopId);
            //可售区域
            addSalesArea(salesAreaVOList, shopId);
            //增加品牌相关
            addShopBrand(brandAuditVOList, shopId);
            //增加默认店铺模板
            templetInfoService.createDefaultTemplateByShopId(shopId);
            //增加默认的店铺合同模板
            shopContractParamRelationService.createDefaultShopContractInfo(shopId);
            //增加入住合同
            addContractSettleIn(shopId);
        } else if (status.equals(ShoppingCodeEnum.AUDIT_RETURM.getCode())) {
            contact = "您的开店申请已打回，审核建议为：" + content;
            smsType = 1;
            contactType = ShoppingCodeEnum.MERCHANT_BUYER.getCode();
        } else {
            //加入黑名单
            contact = "您的开店申请已被拒绝";
            smsType = 1;
            contactType = ShoppingCodeEnum.MERCHANT_BUYER.getCode();
            //添加黑名单
            addBlcakList(sysUserId, auditId, mobile, base.getName().getValue(), ShoppingCodeEnum.BLACKLIST_OPEN.getCode(), shopId);
        }
        //更新店铺表
        updateShopStatus(base, shopId, status);
        //更新审核表
        updateAudit(shopVO, sysUserId, auditId, status, content);
        //发送短信
        addOpenSms(mobile, smsType);
        //发站内信
        sendurgeSignContact(contact, shopId, contactType);
       // List<EsShopDTO> esShopDTOList = shopInfoService.queryEsShop(String.valueOf(shopId));
        //shopSearchService.addShop("index_shop_2020", "shop", 0, esShopDTOList.size(), esShopDTOList);
        return new Result();
    }

    private void updateShopStatus(RegisterShopVO.BaseInfoVO base, Long shopId, Integer status) {
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        shopInfoDTO.setId(shopId);
        shopInfoDTO.setSellerStatus(status);//开店审核状态 0创建、1审核通过、2审核打回、3，审核不通过。4重新提交。
        if (status.equals(ShoppingCodeEnum.AUDIT_PASS.getCode())) {
            shopInfoDTO.setDomain(base.getDomain().getValue());//二级域名
            shopInfoDTO.setType(Integer.parseInt(base.getType().getValue()));//店铺类型 0专营店、1专卖店、2旗舰店、3自营店
            shopInfoDTO.setShopName(base.getShopName().getValue());//店铺名称
            shopInfoDTO.setShopAbbreviation(base.getShopAbbreviation().getValue());//店铺简称
            shopInfoDTO.setShopRealName(base.getShopRealName().getValue());//店铺本名
            shopInfoDTO.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(base.getShopName().getValue()));//店铺名首字母缩写
            shopInfoDTO.setMerchantType(ShoppingCodeEnum.MERCHANT_SELL.getCode());//店铺类别 0买家，1卖家
            // 将店铺加入ES
            List<EsShopDTO> esShop = iShopInfoService.queryEsShop(shopId.toString());
            shopSearchService.updateShop(shopId.toString(), "index_shop_2020", "", 0, esShop.size(), esShop);
        }
        iShopInfoService.updateById(shopInfoDTO);
    }

    @Override
    public Result insertchangeAudit(String shopVO) {
        RegisterShopVO registerShopVO = JSONObject.parseObject(shopVO, RegisterShopVO.class);
        RegisterShopVO.AuditInfoVO audit = registerShopVO.getAudit();//审核内容
        RegisterShopVO.BaseInfoVO base = registerShopVO.getBase();//基础信息
        List<RegisterShopVO.QualificationInfoVO> qualificationInfoVOList = registerShopVO.getQualificationInfoVOList();//资质

        String suId = audit.getSysUserId().getValue();
        String aId = audit.getAuditId().getValue();
        String astatus = audit.getStatus().getValue();
        String sId = base.getShopId().getValue();
        String content = audit.getContent().getValue();
        String idCard = "";
        if (base.getIdCard() != null) {
            idCard = base.getIdCard().getValue();
        }
        if (StringUtil.isEmpty(suId) || StringUtil.isEmpty(astatus) || StringUtil.isEmpty(sId) || StringUtil.isEmpty(aId)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

         Long sysUserId = Long.parseLong(suId);//系统审核人员ID
        Integer status = Integer.parseInt(astatus);//审核状态
        Long shopId = Long.parseLong(sId);//店铺ID
        Long auditId = Long.parseLong(aId);//审核表ID
        Integer merchantType = iShopInfoService.selectById(shopId).getMerchantType();
        String Contact = "您的资质变更申请已打回，审核意见：" + audit.getContent().getValue();
        AuditDTO dto = auditService.selectById(auditId);
        if (!dto.getStatus().equals(0) && !dto.getStatus().equals(4)) {
            return new Result<>(CodeEnum.FAIL_100046);
        }
        if (status.equals(ShoppingCodeEnum.AUDIT_PASS.getCode())) {
            //查询审核日志表
            AuditLogDTO auditLogDTO = auditLogService.getByAidAndStatus(auditId, status);
            if (auditLogDTO != null) {
                return new Result<>(CodeEnum.FAIL_100046);
            } else {
                auditLogService.add(auditId, status);//此时的状态是未通过
            }
            //更新资质表
            updateChangeQ(qualificationInfoVOList, shopId, sysUserId);
            //自动解冻
            iShopInfoService.autoUnfreeze(shopId);

            Contact = "您的资质变更申请已通过！";
            // 护照号码不为空则更新护照号码
            if (StringUtil.isNotEmpty(idCard)) {
                ShopInfoDTO updateDto = new ShopInfoDTO();
                updateDto.setId(shopId);
                updateDto.setIdCard(idCard);
                iShopInfoService.updateById(updateDto);
            }
        }
        //更新审核表
        updateAudit(shopVO, sysUserId, auditId, status, content);
        urgeSignContact(Contact, shopId, merchantType);
        return new Result<>();
    }

    @Override
    public Result insertaddBAudit(String shopVO) {
        RegisterShopVO registerShopVO = JSONObject.parseObject(shopVO, RegisterShopVO.class);
        RegisterShopVO.AuditInfoVO audit = registerShopVO.getAudit();//审核内容
        RegisterShopVO.BaseInfoVO base = registerShopVO.getBase();//基础信息
        List<RegisterShopVO.BrandAuditVO> brandAuditVOList = registerShopVO.getBrandAuditVOList();//品牌

        String suId = audit.getSysUserId().getValue();
        String aId = audit.getAuditId().getValue();
        String astatus = audit.getStatus().getValue();
        String sId = base.getShopId().getValue();
        String content = audit.getContent().getValue();

        if (StringUtil.isEmpty(suId) || StringUtil.isEmpty(astatus) || StringUtil.isEmpty(sId) || StringUtil.isEmpty(aId)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Long sysUserId = Long.parseLong(suId);//系统审核人员ID
        Integer status = Integer.parseInt(astatus);//审核状态
        Long shopId = Long.parseLong(sId);//店铺ID
        Long auditId = Long.parseLong(aId);//审核表ID


        String Contact = "您的新增品牌申请已打回，审核意见：" + audit.getContent().getValue();
        AuditDTO dto = auditService.selectById(auditId);
        if (!dto.getStatus().equals(0) && !dto.getStatus().equals(4)) {
            return new Result<>(CodeEnum.FAIL_100046);
        }
        if (status.equals(ShoppingCodeEnum.AUDIT_PASS.getCode())) {
            //查询审核日志表
            AuditLogDTO auditLogDTO = auditLogService.getByAidAndStatus(auditId, status);
            if (auditLogDTO != null) {
                return new Result<>(CodeEnum.FAIL_100046);
            } else {
                auditLogService.add(auditId, status);//此时的操作状态是未通过
            }
            addShopBrand(brandAuditVOList, shopId);
            Contact = "您的新增品牌申请已通过！";
        }
        //更新审核表
        updateAudit(shopVO, sysUserId, auditId, status, content);
        urgeSignContact(Contact, shopId, ShoppingCodeEnum.MERCHANT_SELL.getCode());
        return new Result<>();
    }

    @Override
    public Result addCategoryAudit(String shopVO) {
        RegisterShopVO registerShopVO = JSONObject.parseObject(shopVO, RegisterShopVO.class);
        RegisterShopVO.AuditInfoVO audit = registerShopVO.getAudit();//审核内容
        RegisterShopVO.BaseInfoVO base = registerShopVO.getBase();//基础信息
        List<RegisterShopVO.BrandAuditVO> brandAuditVOList = registerShopVO.getBrandAuditVOList();//品牌

        String suId = audit.getSysUserId().getValue();
        String aId = audit.getAuditId().getValue();
        String astatus = audit.getStatus().getValue();
        String sId = base.getShopId().getValue();
        String content = audit.getContent().getValue();

        if (StringUtil.isEmpty(suId) || StringUtil.isEmpty(astatus) || StringUtil.isEmpty(sId) || StringUtil.isEmpty(aId)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        log.info ("----addCategoryAudit------sessionId:" + CommonParameter.getHeadParamsBO ().getSessionId ());

        Long sysUserId = Long.parseLong(suId);//系统审核人员ID
        Integer status = Integer.parseInt(astatus);//审核状态
        Long shopId = Long.parseLong(sId);//店铺ID
        Long auditId = Long.parseLong(aId);//审核表ID
        AuditDTO dto = auditService.selectById(auditId);
        if (!dto.getStatus().equals(0) && !dto.getStatus().equals(4)) {
            return new Result<>(CodeEnum.FAIL_100046);
        }
        String Contact = "您的新增类目申请已打回，审核意见：" + audit.getContent().getValue();
        if (status.equals(ShoppingCodeEnum.AUDIT_PASS.getCode())) {
            //查询审核日志表
            AuditLogDTO auditLogDTO = auditLogService.getByAidAndStatus(auditId, status);
            if (auditLogDTO != null) {
                return new Result<>(CodeEnum.FAIL_100046);
            } else {
                auditLogService.add(auditId, status);//此时的操作状态是未通过
            }

            for (RegisterShopVO.BrandAuditVO vo : brandAuditVOList) {
                String bId = vo.getId().getValue();
                if (StringUtil.isEmpty(bId)) {
                    continue;
                }
                Long brandId = Long.parseLong(bId);
                addBrandCategory(shopId, brandId, vo.getBrandCategoryVOList());
            }

            Contact = "您的新增类目申请通过！";
        }
        //更新审核表
        updateAudit(shopVO, sysUserId, auditId, status, content);
        urgeSignContact(Contact, shopId, ShoppingCodeEnum.MERCHANT_SELL.getCode());
        return new Result<>();
    }

    @Override
    public Result<?> updateShopAudit(String shopVO) {
        RegisterShopVO registerShopVO = JSONObject.parseObject(shopVO, RegisterShopVO.class);
        RegisterShopVO.AuditInfoVO audit = registerShopVO.getAudit();//审核内容
        RegisterShopVO.BaseInfoVO base = registerShopVO.getBase();//基础信息
        List<RegisterShopVO.SalesAreaVO> salesAreaVOList = registerShopVO.getSalesAreaVOList();//可售区域
        List<RegisterShopVO.BrandAuditVO> brandAuditVOList = registerShopVO.getBrandAuditVOList();//品牌
        String suId = audit.getSysUserId().getValue();
        String aId = audit.getAuditId().getValue();
        String astatus = audit.getStatus().getValue();
        String sId = base.getShopId().getValue();
        String content = audit.getContent().getValue();
        if (StringUtil.isEmpty(suId) || StringUtil.isEmpty(astatus) || StringUtil.isEmpty(sId) || StringUtil.isEmpty(aId)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Long sysUserId = Long.parseLong(suId);//系统审核人员ID
        Integer status = Integer.parseInt(astatus);//审核状态
        Long shopId = Long.parseLong(sId);//店铺ID
        Long auditId = Long.parseLong(aId);//审核表ID
        AuditDTO dto = auditService.selectById(auditId);
        if (!dto.getStatus().equals(0) && !dto.getStatus().equals(4)) {
            return new Result<>(CodeEnum.FAIL_100046);
        }
        //查手机号
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", iShopInfoService.selectById(shopId).getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        String mobile = userLoginDTO.getMobile();
        String contact = "您的店铺变更审核已通过！";
        int smsType = 0;
        int contactType = ShoppingCodeEnum.MERCHANT_SELL.getCode();
        if (status.equals(1)) {
            //判断是否包含审核不过的情况
            if (shopVO.contains("\"status\": \"0\"") || shopVO.contains("\"status\": \"2\"") || shopVO.contains("\"status\": \"3\"")) {
                return new Result<>(CodeEnum.FAIL_100052);
            }
            //查询审核日志表
            AuditLogDTO auditLogDTO = auditLogService.getByAidAndStatus(auditId, status);
            if (auditLogDTO != null) {
                return new Result<>(CodeEnum.FAIL_100046);
            } else {
                auditLogService.add(auditId, status);//此时的状态是未通过
            }
            // 处理可售区域
            this.shopChangeUpdateSalesArea(salesAreaVOList, shopId);
            // 品牌资质
            this.shopChangeUpdateBrandQualifications(brandAuditVOList, shopId);
        } else if (status.equals(ShoppingCodeEnum.AUDIT_RETURM.getCode())) {
            contact = "您的店铺变更申请已打回，审核建议为：" + content;
            smsType = 1;
            contactType = ShoppingCodeEnum.MERCHANT_BUYER.getCode();
        } else {
            //加入黑名单
            contact = "您的店铺变更申请已被拒绝";
            smsType = 1;
            contactType = ShoppingCodeEnum.MERCHANT_BUYER.getCode();
        }
        //更新店铺表
        this.shopChangeUpdateShopInfo(base, shopId, status);
        //更新审核表
        this.updateAudit(shopVO, sysUserId, auditId, status, content);
        //发送短信
        this.addOpenSms(mobile, smsType);
        //发站内信
        this.sendurgeSignContact(contact, shopId, contactType);
        return new Result<>(CodeEnum.SUCCESS);
    }


    /**
     * @return : void
     * @Author :chenyu.wang
     * @Description : 品牌资质
     * @Date : 2021.01.26 15:25
     * @Param : [brandAuditVOList, shopId]
     **/
    private void shopChangeUpdateBrandQualifications(List<RegisterShopVO.BrandAuditVO> brandAuditVOList, Long shopId) {
        //删除原来的
        Wrapper<ShopBrandRelationDTO> shopBrandRelationDTOWrapper = new EntityWrapper<>();
        shopBrandRelationDTOWrapper.eq("shop_id", shopId);
        List<ShopBrandRelationDTO> brandRelationDTOS = iShopBrandRelationService.selectList(shopBrandRelationDTOWrapper);
        if (CustomizeUtil.listIsNotNull(brandRelationDTOS)) {
            for (ShopBrandRelationDTO brandRelationDTO : brandRelationDTOS) {
                this.lowerFrame(shopId, brandRelationDTO.getBrandId());
                iShopBrandRelationService.deleteById(brandRelationDTO.getId());
            }
        }
        // 将原来的店铺类目关联表软删除
        Wrapper<ShopCategoryRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId);
        List<ShopCategoryRelationDTO> list = iShopCategoryRelationService.selectList(wrapper);
        if (CustomizeUtil.listIsNotNull(list)) {
            for (ShopCategoryRelationDTO dto : list) {
                // 已删除
                dto.setIsDelete(1);
                iShopCategoryRelationService.updateById(dto);
            }
        }
        for (RegisterShopVO.BrandAuditVO vo : brandAuditVOList) {
            ShopBrandRelationDTO shopBrandRelationDTO = new ShopBrandRelationDTO();
            /*
             * 判断品牌是否存在，存在正常保存，不存在先保存品牌
             */
            Integer addStatus = Integer.parseInt(vo.getAddStatus().getValue());
            Long brandId;
            if (addStatus.equals(ShoppingCodeEnum.AUDIT_BRAND_NULL.getCode())) {
                //检测是否有同名的 没有就新增，有就用同名的id
                String brandName = vo.getName().getValue().trim();
                BrandInfoDTO oldBrandInfoDTO = iBrandInfoService.getByName(brandName);
                if (oldBrandInfoDTO == null) {
                    BrandInfoDTO brandInfoDTO = new BrandInfoDTO();
                    brandId = ymlConfig.getPkId();
                    brandInfoDTO.setId(brandId);
                    brandInfoDTO.setName(brandName);
                    brandInfoDTO.setLogoUrl(vo.getLogoUrl().getValue());
                    brandInfoDTO.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(brandName));
                    brandInfoDTO.setIsPublish(1);//是否发布;0未发布，1发布
                    iBrandInfoService.insert(brandInfoDTO);
                } else {
                    brandId = oldBrandInfoDTO.getId();
                }
            } else {
                brandId = Long.parseLong(vo.getId().getValue());
            }
            //保存关联表
            shopBrandRelationDTO.setId(ymlConfig.getPkId());
            shopBrandRelationDTO.setShopId(shopId);
            shopBrandRelationDTO.setBrandId(brandId);
            iShopBrandRelationService.insert(shopBrandRelationDTO);
            //修改品牌资质
            this.updateShopQualification(vo.getQualificationInfoVOList(), shopId, brandId);
            //保存品牌分类表
            this.updateBrandCategory(shopId, brandId, vo.getBrandCategoryVOList());
        }
    }

    /**
     * @return : void
     * @Author :chenyu.wang
     * @Description : 修改品牌分类
     * @Date : 2021.01.26 15:53
     * @Param : [shopId, brandId, brandCategoryVOList]
     **/
    private void updateBrandCategory(Long shopId, Long brandId, List<RegisterShopVO.BrandCategoryVO> brandCategoryVOList) {
        for (RegisterShopVO.BrandCategoryVO brandCategoryVO : brandCategoryVOList) {
            String[] cIds = brandCategoryVO.getCategoryId().getValue().split(",");
            String cId = cIds[cIds.length - 1];
            Long categoryId = Long.parseLong(cId);
            ShopCategoryRelationDTO shopCategoryRelationDTO = new ShopCategoryRelationDTO();
            shopCategoryRelationDTO.setId(ymlConfig.getPkId());
            shopCategoryRelationDTO.setBrandId(brandId);//已经存在的id
            shopCategoryRelationDTO.setCategoryId(categoryId);
            shopCategoryRelationDTO.setShopId(shopId);
            CommodityCategoryDTO dto = commodityCategoryService.selectById (categoryId);
            log.info ("设置佣金比例+++++++++++++++++++++++++++++++++" +dto.getDividendScale ());
            shopCategoryRelationDTO.setDividendScale (dto.getDividendScale ());
            iShopCategoryRelationService.insert(shopCategoryRelationDTO);
            //保存关系单位表
            iCategoryBrandAttributesRelationService.addUnitidsByBrandAndCategory(brandId, categoryId);
        }
    }

    /**
     * @return : void
     * @Author :chenyu.wang
     * @Description : 修改品牌资质
     * @Date : 2021.01.26 15:50
     * @Param : [qList, shopId, brandId]
     **/
    private void updateShopQualification(List<RegisterShopVO.QualificationInfoVO> qList, Long shopId, Long brandId) {
        Wrapper<ShopQualificationDTO> wrapperA = new EntityWrapper<>();
        wrapperA.eq("shop_id", shopId).eq("brand_id", brandId);
        iShopQualificationService.delete(wrapperA);
        for (RegisterShopVO.QualificationInfoVO qvo : qList) {
            int type = Integer.parseInt(qvo.getQtype().getValue());
            ShopQualificationDTO shopQualificationDTO = new ShopQualificationDTO();
            shopQualificationDTO.setId(ymlConfig.getPkId());
            shopQualificationDTO.setShopId(shopId);
            shopQualificationDTO.setBrandId(brandId);
            shopQualificationDTO.setFileUrl(qvo.getFileUrl().getValue());
            shopQualificationDTO.setType(type);
            shopQualificationDTO.setBeginDate(DateUtils.getDateByStr(qvo.getBeginDate().getValue()));
            shopQualificationDTO.setEndDate(DateUtils.getDateByStr(qvo.getEndDate().getValue()));
            iShopQualificationService.insert(shopQualificationDTO);
        }
    }

    /**
     * @return : void
     * @Author :chenyu.wang
     * @Description : 取消订单
     * @Date : 2021.01.26 21:01
     * @Param : [goodsId]
     **/
    public void cancelOrder(Long goodsId) {
        Wrapper<OrderCommodityRelationDTO> wrapperOrderGoods = new EntityWrapper<>();
        wrapperOrderGoods.eq("commodity_id", goodsId);
        List<OrderCommodityRelationDTO> orderGoodsItem = orderCommodityRelationService.selectList(wrapperOrderGoods);
        if (CustomizeUtil.listIsNull(orderGoodsItem)) {
            return;
        }
        for (OrderCommodityRelationDTO relationDTO : orderGoodsItem) {
            // 订单id
            Long orderId = relationDTO.getOrderId();
            // 获取订单状态
            Wrapper<OrderStatusDTO> statusDTOWrapper = new EntityWrapper<>();
            statusDTOWrapper.eq("order_id", orderId);
            OrderStatusDTO statusDTO = orderStatusService.selectOne(statusDTOWrapper);
            if (statusDTO == null) {
                continue;
            }
            if (statusDTO.getOrderState() <= 3) {
                try {
                    orderInfoService.cancelOrder(statusDTO.getOrderId(), 7, "");
                } catch (Exception e) {
                    log.info("店铺变更取消订单错误信息:{}", e.getMessage());
                    e.printStackTrace();
                }

            }
        }
    }

    /**
     * @return : void
     * @Author :chenyu.wang
     * @Description : 下架商品
     * @Date : 2021.01.27 09:47
     * @Param : [shopId, brandId]
     **/
    private void lowerFrame(Long shopId, Long brandId) {
        // 先查询是否存在品牌关联的商品
        Wrapper<CommodityInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId);
        wrapper.eq("brand_id", brandId);
        List<CommodityInfoDTO> goodsList = commodityInfoService.selectList(wrapper);
        if (CustomizeUtil.listIsNull(goodsList)) {
            return;
        }
        try {
            for (CommodityInfoDTO commodityInfoDTO : goodsList) {
                // 商品ID
                Long goodsId = commodityInfoDTO.getId();
                // 下架商品
                commodityInfoDTO.setIsPublish(0);
                commodityInfoService.updateById(commodityInfoDTO);
                // es中删除商品
                searchService.deleteData("index_goods_2020", "goods", goodsId.toString());
                // 取消订单
                this.cancelOrder(goodsId);
            }
        } catch (Exception e) {
            log.error("店铺变更下架商品错误信息:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * @return : void
     * @Author :chenyu.wang
     * @Description : 店铺变更更改可售区域
     * @Date : 2021.01.26 15:06
     * @Param : [salesAreaVOList, shopId]
     **/
    private void shopChangeUpdateSalesArea(List<RegisterShopVO.SalesAreaVO> salesAreaVOList, Long shopId) {
        // 先删除原来的可售区域
        Wrapper<ShopSalesAreaDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId);
        List<ShopSalesAreaDTO> list = shopSalesAreaService.selectList(wrapper);
        if (CustomizeUtil.listIsNotNull(list)) {
            shopSalesAreaService.delete(wrapper);
        }
        for (RegisterShopVO.SalesAreaVO svo : salesAreaVOList) {
            ShopSalesAreaSaveBO shopSalesAreaSaveBO = new ShopSalesAreaSaveBO();
            shopSalesAreaSaveBO.setId(ymlConfig.getPkId());
            shopSalesAreaSaveBO.setShopId(shopId);
            shopSalesAreaSaveBO.setAreaId(Long.parseLong(svo.getAreaId().getValue()));
            shopSalesAreaService.add(shopSalesAreaSaveBO);
        }
    }

    /**
     * @return : void
     * @Author :chenyu.wang
     * @Description : 店铺变更修改店铺基本信息
     * @Date : 2021.01.26 14:20
     * @Param : [shopBase, shopId, status]
     **/
    private void shopChangeUpdateShopInfo(RegisterShopVO.BaseInfoVO shopBase, Long shopId, Integer status) {
        // 二级域名
        String newDomain = shopBase.getDomain().getValue();
        // 店铺类型
        String newShopType = shopBase.getType().getValue();
        // 店铺名称
        String newShopName = shopBase.getShopName().getValue();
        ShopInfoDTO updateDto = new ShopInfoDTO();
        updateDto.setId(shopId);
        if (status.equals(1)) {
            // 审核通过
            updateDto.setDomain(newDomain);
            updateDto.setType(Integer.valueOf(newShopType));
            updateDto.setShopName(newShopName);
            updateDto.setChangeStatus(1);
            updateDto.setChangeTime(new Date());
            // 将店铺加入ES
            List<EsShopDTO> esShop = iShopInfoService.queryEsShop(shopId.toString());
            shopSearchService.updateShop(shopId.toString(), "index_shop_2020", "", 0, esShop.size(), esShop);
        } else if (status.equals(2)) {
            // 审核打回
            updateDto.setChangeStatus(3);
        } else {
            // 审核不通过
            updateDto.setChangeStatus(0);
        }
        iShopInfoService.updateById(updateDto);
    }


    private void updateChangeQ(List<RegisterShopVO.QualificationInfoVO> qualificationInfoVOList, Long shopId, Long sysUserId) {
        for (RegisterShopVO.QualificationInfoVO vo : qualificationInfoVOList) {

            Wrapper<ShopQualificationDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("shop_id", shopId).eq("type", vo.getQtype().getValue());
            if (!"".equals(vo.getBrandId().getValue())) {
                wrapper.eq("brand_id", vo.getBrandId().getValue());
            }

            ShopQualificationDTO shopQualificationDTO = iShopQualificationService.selectOne(wrapper);
            if (shopQualificationDTO == null) {
                if (vo.getQtype().getValue().equals(String.valueOf(ShoppingCodeEnum.QUALIFICATION_RENEWAL.getCode()))) {
                    addSQualification(vo, shopId);
                }
                continue;
            }
            ShopQualificationDTO newDTO = new ShopQualificationDTO();
            newDTO.setId(shopQualificationDTO.getId());
            newDTO.setFileUrl(vo.getFileUrl().getValue());//地址
            newDTO.setType(shopQualificationDTO.getType());
            newDTO.setShopId(shopQualificationDTO.getShopId());
            if ("".equals(vo.getBeginDate().getValue())) {
                newDTO.setBeginDate(null);
            } else {
                newDTO.setBeginDate(DateUtils.getDateByStr(vo.getBeginDate().getValue()));
            }

            if ("".equals(vo.getEndDate().getValue())) {
                newDTO.setEndDate(null);
            } else {
                newDTO.setEndDate(DateUtils.getDateByStr(vo.getEndDate().getValue()));
            }
            newDTO.setExpireStatus(ShoppingCodeEnum.EXPIRE_DEFAULT.getCode());
            newDTO.setUpdateUserId(sysUserId);//更新人

            newDTO = updateSbQualificationDTO(newDTO);

            iShopQualificationService.updateById(newDTO);
        }
    }

    private void addSalesArea(List<RegisterShopVO.SalesAreaVO> salesAreaVOList, Long shopId) {
        for (RegisterShopVO.SalesAreaVO svo : salesAreaVOList) {
            ShopSalesAreaSaveBO shopSalesAreaSaveBO = new ShopSalesAreaSaveBO();
            shopSalesAreaSaveBO.setId(ymlConfig.getPkId());
            shopSalesAreaSaveBO.setShopId(shopId);
            shopSalesAreaSaveBO.setAreaId(Long.parseLong(svo.getAreaId().getValue()));
            shopSalesAreaService.add(shopSalesAreaSaveBO);
        }
    }

    private void addShopBrand(List<RegisterShopVO.BrandAuditVO> brandAuditVOList, Long shopId) {
        for (RegisterShopVO.BrandAuditVO vo : brandAuditVOList) {
            ShopBrandRelationDTO shopBrandRelationDTO = new ShopBrandRelationDTO();
            /*
             * 判断品牌是否存在，存在正常保存，不存在先保存品牌
             */
            Integer addStatus = Integer.parseInt(vo.getAddStatus().getValue());

            Long brandId = null;
            if (addStatus.equals(ShoppingCodeEnum.AUDIT_BRAND_NULL.getCode())) {
                //检测是否有同名的 没有就新增，有就用同名的id
                String brandName = vo.getName().getValue().trim();
                BrandInfoDTO oldBrandInfoDTO = iBrandInfoService.getByName(brandName);
                if (oldBrandInfoDTO == null) {
                    BrandInfoDTO brandInfoDTO = new BrandInfoDTO();
                    brandId = ymlConfig.getPkId();
                    brandInfoDTO.setId(brandId);
                    brandInfoDTO.setName(brandName);
                    brandInfoDTO.setLogoUrl(vo.getLogoUrl().getValue());
                    brandInfoDTO.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(brandName));
                    brandInfoDTO.setIsPublish(1);//是否发布;0未发布，1发布
                    iBrandInfoService.insert(brandInfoDTO);
                } else {
                    brandId = oldBrandInfoDTO.getId();
                }
            } else {
                brandId = Long.parseLong(vo.getId().getValue());
            }

            //新增品牌要验证是否关联过
            Wrapper<ShopBrandRelationDTO> bwrapper = new EntityWrapper<>();
            bwrapper.eq("shop_id", shopId).eq("brand_id", brandId);
            ShopBrandRelationDTO oldShopBrandRelationDTO = iShopBrandRelationService.selectOne(bwrapper);
            if (oldShopBrandRelationDTO != null && oldShopBrandRelationDTO.getId() != null) {
                continue;
            }

            //保存关联表
            shopBrandRelationDTO.setId(ymlConfig.getPkId());
            shopBrandRelationDTO.setShopId(shopId);
            shopBrandRelationDTO.setBrandId(brandId);
            iShopBrandRelationService.insert(shopBrandRelationDTO);

            //保存品牌资质
            addShopQualification(vo.getQualificationInfoVOList(), shopId, brandId);

            //保存品牌分类表
            addBrandCategory(shopId, brandId, vo.getBrandCategoryVOList());
        }
    }

    private void addBrandCategory(Long shopId, Long brandId, List<RegisterShopVO.BrandCategoryVO> brandCategoryVOList) {
        for (RegisterShopVO.BrandCategoryVO brandCategoryVO : brandCategoryVOList) {
            String[] cIds = brandCategoryVO.getCategoryId().getValue().split(",");
            String cId = cIds[cIds.length - 1];
            Long categoryId = Long.parseLong(cId);
            ShopCategoryRelationDTO shopCategoryRelationDTO = new ShopCategoryRelationDTO();
            shopCategoryRelationDTO.setId(ymlConfig.getPkId());
            shopCategoryRelationDTO.setBrandId(brandId);//已经存在的id
            shopCategoryRelationDTO.setCategoryId(categoryId);
            shopCategoryRelationDTO.setShopId(shopId);
            CommodityCategoryDTO dto = commodityCategoryService.selectById(categoryId);
            if(dto != null){
                shopCategoryRelationDTO.setDividendScale(dto.getDividendScale());
            }

            iShopCategoryRelationService.insert(shopCategoryRelationDTO);

            //保存关系单位表
            iCategoryBrandAttributesRelationService.addUnitidsByBrandAndCategory(brandId, categoryId);
        }
    }

    private void addAnnualAndEarnest(BigDecimal earnestMoney, BigDecimal annualFee, Long shopId) {
        /*
         * 保证金初始化
         */
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        shopInfoDTO.setId(shopId);
        ShopEarnestMoneySaveBO shopEarnestMoneySaveBO = new ShopEarnestMoneySaveBO();
        if (earnestMoney.compareTo(BigDecimal.ZERO) == 0) {
            shopEarnestMoneySaveBO.setStatus(ShoppingCodeEnum.MONEY_PAID.getCode()); //状态 0未缴纳，1已缴纳，2保证金不足
            shopInfoDTO.setEarnestMoney(BigDecimal.ZERO);
            shopInfoDTO.setMarginStatus(ShoppingCodeEnum.MONEY_PAID.getCode());//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
        } else {
            shopEarnestMoneySaveBO.setPayable(earnestMoney);//应交
            shopEarnestMoneySaveBO.setVacancyAmount(earnestMoney);//需补足
            shopInfoDTO.setEarnestMoney(BigDecimal.ZERO);
            shopEarnestMoneySaveBO.setStatus(ShoppingCodeEnum.MONEY_UNPAID.getCode()); //状态 0未缴纳，1已缴纳，2保证金不足
        }
        shopEarnestMoneySaveBO.setShopId(shopId);//店铺ID
        iShopEarnestMoneyService.add(shopEarnestMoneySaveBO);

        /*
         * 年费初始化
         */
        ShopAnnualFeeSaveBO shopAnnualFeeSaveBO = new ShopAnnualFeeSaveBO();
        if (annualFee.compareTo(BigDecimal.ZERO) == 0) {
            shopAnnualFeeSaveBO.setStatus(ShoppingCodeEnum.MONEY_PAID.getCode()); //状态 0未缴纳，1已缴纳，2保证金不足
            shopInfoDTO.setAnnualFeeStatus(ShoppingCodeEnum.MONEY_PAID.getCode());//年费 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
        } else {
            shopAnnualFeeSaveBO.setPayable(annualFee);//应交
            shopAnnualFeeSaveBO.setVacancyAmount(annualFee);//需补足
            shopAnnualFeeSaveBO.setStatus(ShoppingCodeEnum.MONEY_UNPAID.getCode()); //状态 0未缴纳，1已缴纳，2保证金不足
        }
        shopAnnualFeeSaveBO.setShopId(shopId);//店铺ID
        iShopAnnualFeeService.add(shopAnnualFeeSaveBO);

        iShopInfoService.updateById(shopInfoDTO);
    }

    private void addReceiveCard(RegisterShopVO.BaseInfoVO base, Long shopId) {
        ShopReceiveCardSaveBO shopReceiveCardSaveBO = new ShopReceiveCardSaveBO();
        shopReceiveCardSaveBO.setShopId(shopId);
        shopReceiveCardSaveBO.setBankName(base.getBankName().getValue());
        shopReceiveCardSaveBO.setBankCardNo(base.getBankCardNo().getValue());
        shopReceiveCardSaveBO.setBankAddress(base.getBankAddress().getValue());
        shopReceiveCardSaveBO.setName(base.getName().getValue());
        shopReceiveCardSaveBO.setType(1);//账户类型 0其他，1开户账户 默认0
        shopReceiveCardService.add(shopReceiveCardSaveBO);
    }

    private void addBlcakList(Long sysUserId, Long shopAuditId, String mobile, String name, Integer type, Long shopId) {
        ShopBlacklistDTO shopBlacklistDTO = new ShopBlacklistDTO();
        shopBlacklistDTO.setCreateUserId(sysUserId);
        shopBlacklistDTO.setShopInfoAuditId(shopAuditId);
        shopBlacklistDTO.setMobile(mobile);
        shopBlacklistDTO.setCompanyName(name);
        shopBlacklistDTO.setType(type);
        shopBlacklistDTO.setShopId(shopId);
        shopBlacklistDTO.setId(ymlConfig.getPkId());
        iShopBlacklistService.insert(shopBlacklistDTO);
    }

    private void sendurgeSignContact(String conunt, Long sellShopId, Integer shopType) {
        MessageAnnouncementSaveBO messageAnnouncementSaveBO = new MessageAnnouncementSaveBO();

        if (shopType.equals(ShoppingCodeEnum.MERCHANT_BUYER.getCode())) {
            //买家
            messageAnnouncementSaveBO.setShopType(MessageEnum.SHOP_TYPE_1.getCode());
        } else {
            messageAnnouncementSaveBO.setShopType(MessageEnum.SHOP_TYPE_2.getCode());
        }

        ResponseDTO responseDTO = new ResponseDTO();
        messageAnnouncementSaveBO.setContent(conunt);
        messageAnnouncementSaveBO.setShopId(sellShopId);
        messageAnnouncementSaveBO.setType(MessageEnum.SYS_MESSAGE.getCode());
        messageAnnouncementSaveBO.setTitle("审核结果通知");
        messageAnnouncementSaveBO.setJumpType(MessageEnum.MESSAGE_SHOW.getCode() + "");
        messageAnnouncementSaveBO.setTextType(1);
        messageAnnouncementSaveBO.setKey(sellShopId + "");
        responseDTO.setData(messageAnnouncementSaveBO);
        responseDTO.setMessageId(sellShopId + "");

        try {
            kafkaProducerService.senMessage(JSON.toJSONString(responseDTO), MessageTopicType.COMMENT_MESSAGE_TOPIC);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void sendLoginSms(String mobile, Integer status) {
        SmsInfoSaveBO bo = new SmsInfoSaveBO();
        bo.setMobile(mobile);
        try {
            if (status.equals(1)) {
                iSmsInfoService.savePassAudit(bo);
            } else {
                iSmsInfoService.saveUnPassAudit(bo);
            }
        } catch (Exception e) {
            log.info(e.toString());
        }

    }

    private void addCiccOpen(RegisterVO.BaseInfoVO baseInfoVO, Long shopId) {
        PayUserSaveBO payUserSaveBO = new PayUserSaveBO();
        payUserSaveBO.setUserId(shopId);
        payUserSaveBO.setUserName(baseInfoVO.getName().getValue());
        payUserSaveBO.setType(1);
        payUserSaveBO.setLegalName(baseInfoVO.getLegalPerson().getValue());
        payUserSaveBO.setLegalCredentialsType(baseInfoVO.getIdCardType().getValue());
        payUserSaveBO.setLegalCredentialsNo(baseInfoVO.getIdCard().getValue());
        payUserSaveBO.setBusinessLicenseNo(baseInfoVO.getCreditCode().getValue());
        payUserSaveBO.setUserAddress(baseInfoVO.getAddress().getValue());
        payUserSaveBO.setUserMobile(baseInfoVO.getMobile().getValue());
        payUserSaveBO.setUserEmail(baseInfoVO.getEmail().getValue());

        Result result = iPayUserService.add(payUserSaveBO);
        ShopInfoDTO newDTO = new ShopInfoDTO();
        newDTO.setId(shopId);
        if (result.getCode() == 0) {
            newDTO.setCiccStatus(1);//中金开户状态 0未开户，1已开户
            ObjectMapper objectMapper = new ObjectMapper();
            PayUserDTO dto = objectMapper.convertValue(result.getData(), PayUserDTO.class);
            newDTO.setZjMoneyNo(dto.getZjMoneyNo());
            newDTO.setZjBankName(dto.getZjBankName());
            iShopInfoService.updateById(newDTO);
        }
    }

    private void addQiyuesuo(RegisterVO.BaseInfoVO baseInfoVO, Long shopId) {
        String name = baseInfoVO.getName().getValue();
        ShopQiyuesuoInfoSaveBO shopQiyuesuoInfoSaveBO = new ShopQiyuesuoInfoSaveBO();
        shopQiyuesuoInfoSaveBO.setShopId(shopId);
        shopQiyuesuoInfoSaveBO.setCompanyName(name);
        shopQiyuesuoInfoSaveBO.setTenantName(name);
        shopQiyuesuoInfoSaveBO.setTenantType("COMPANY");
        shopQiyuesuoInfoSaveBO.setContact(baseInfoVO.getMobile().getValue());
        shopQiyuesuoInfoSaveBO.setContactType("MOBILE");
        shopQiyuesuoInfoSaveBO.setTenantStamperType("COMPANY");
        shopQiyuesuoInfoSaveBO.setAuthenticationStatus("-1");
        shopQiyuesuoInfoSaveBO.setLegalPerson(baseInfoVO.getLegalPerson().getValue());
        shopQiyuesuoInfoSaveBO.setRegisterNo(baseInfoVO.getCreditCode().getValue());
        iShopQiyuesuoInfoService.add(shopQiyuesuoInfoSaveBO);
    }

    private void addSQualification(RegisterShopVO.QualificationInfoVO vo, Long shopId) {
        ShopQualificationDTO shopQualificationDTO = new ShopQualificationDTO();
        shopQualificationDTO.setShopId(shopId);
        shopQualificationDTO.setId(ymlConfig.getPkId());
        shopQualificationDTO.setFileUrl(vo.getFileUrl().getValue());
        shopQualificationDTO.setType(Integer.parseInt(vo.getQtype().getValue()));
        shopQualificationDTO.setBeginDate(DateUtils.getDateByStr(vo.getBeginDate().getValue()));
        shopQualificationDTO.setEndDate(DateUtils.getDateByStr(vo.getEndDate().getValue()));
        iShopQualificationService.insert(shopQualificationDTO);
    }

    private void addRQualification(List<RegisterVO.QualificationInfoVO> qList, Long shopId) {
        for (RegisterVO.QualificationInfoVO qvo : qList) {
            ShopQualificationDTO shopQualificationDTO = new ShopQualificationDTO();
            shopQualificationDTO.setShopId(shopId);
            shopQualificationDTO.setId(ymlConfig.getPkId());
            shopQualificationDTO.setFileUrl(qvo.getFileUrl().getValue());
            shopQualificationDTO.setType(Integer.parseInt(qvo.getQtype().getValue()));
            shopQualificationDTO.setBeginDate(DateUtils.getDateByStr(qvo.getBeginDate().getValue()));
            shopQualificationDTO.setEndDate(DateUtils.getDateByStr(qvo.getEndDate().getValue()));
            iShopQualificationService.insert(shopQualificationDTO);
        }
    }

    private void addShopQualification(List<RegisterShopVO.QualificationInfoVO> qList, Long shopId, Long brandId) {
        for (RegisterShopVO.QualificationInfoVO qvo : qList) {
            ShopQualificationDTO shopQualificationDTO = new ShopQualificationDTO();
            shopQualificationDTO.setId(ymlConfig.getPkId());
            shopQualificationDTO.setShopId(shopId);
            shopQualificationDTO.setBrandId(brandId);
            shopQualificationDTO.setFileUrl(qvo.getFileUrl().getValue());
            shopQualificationDTO.setType(Integer.parseInt(qvo.getQtype().getValue()));
            shopQualificationDTO.setBeginDate(DateUtils.getDateByStr(qvo.getBeginDate().getValue()));
            shopQualificationDTO.setEndDate(DateUtils.getDateByStr(qvo.getEndDate().getValue()));
            iShopQualificationService.insert(shopQualificationDTO);
        }
    }

    private void addShopUserRela(Long shopId, Long userId) {
        ShopUserRelationDTO shopUserRelationDTO = new ShopUserRelationDTO();
        Long shopUserRelationId = ymlConfig.getPkId();
        shopUserRelationDTO.setId(shopUserRelationId);
        shopUserRelationDTO.setUserId(userId);
        shopUserRelationDTO.setShopId(shopId);
        iShopUserRelationService.insert(shopUserRelationDTO);
    }

    private Long addShopInfo(RegisterVO.BaseInfoVO baseInfoVO, Long userId) {
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        Long shopInfoId = ymlConfig.getPkId();
        shopInfoDTO.setId(shopInfoId);
        shopInfoDTO.setUserId(userId);//保存用户ID
        shopInfoDTO.setName(baseInfoVO.getName().getValue());//公司名称
        shopInfoDTO.setAddress(baseInfoVO.getAddress().getValue());//保存公司地址
        shopInfoDTO.setInvitationCode(baseInfoVO.getInvitationCode().getValue());//邀请码
        shopInfoDTO.setLegalPerson(baseInfoVO.getLegalPerson().getValue());//法人
        shopInfoDTO.setIdCard(baseInfoVO.getIdCard().getValue());//法人身份证号
        shopInfoDTO.setIdCardType(baseInfoVO.getIdCardType().getValue());//法人证件类型
        shopInfoDTO.setCreditCode(baseInfoVO.getCreditCode().getValue());//统一信用代码
        shopInfoDTO.setPayMobile(baseInfoVO.getPayMobile().getValue());
        iShopInfoService.insert(shopInfoDTO);//店铺类型默认为买家

        return shopInfoId;
    }

    //更新用户表
    private void updateUserInfo(Long sysUserId, Long userId, Integer status) {
        UserInfoDTO userDTO = new UserInfoDTO();
        userDTO.setId(userId);
        userDTO.setStatus(status);//状态 0创建，1审核通过，2审核打回，3审核不通过,4冻结、5删除
        userDTO.setUpdateUserId(sysUserId);
        iUserInfoService.updateById(userDTO);
    }

    //更新审核表
    private void updateAudit(String loginVO, Long sysUserId, Long shopAuditId, Integer status, String content) {
        AuditDTO auditDTO = new AuditDTO();
        auditDTO.setId(shopAuditId);

        log.info ("-----updateAudit-----sessionId:" + CommonParameter.getHeadParamsBO ().getSessionId ());
        Long userId = null;
        Object object = redisUtil.getSession (CommonParameter.getHeadParamsBO ().getSessionId (), CommonParameter.getHeadParamsBO ().getSource ().toString ());
        if(object instanceof UserInfoDTO){
            userId = ((UserInfoDTO) object).getId ();
        } if(object instanceof SysUserDTO){
            userId = ((SysUserDTO) object).getId ();
        }
        auditDTO.setCreateUserId(userId);
        auditDTO.setParams(loginVO);
        auditDTO.setStatus(status);
        auditDTO.setContent(content);
        auditService.update(auditDTO);
    }

}
